Question

I have an index of ordered stories:

{
  index: ["storyA", "storyB", "storyC"]
}

I want to get the referenced stories and pass it into angularFireCollection

{
  index: ["storyA", "storyB", "storyC"],
  stories: {
    "storyA" : {},
    "storyB" : {},
    "storyC" : {},
    "storyD" : {},
    "storyE" : {}
  },
}

This is the example from angularfire. This would get all the stories instead of just the ones in the index. How do I get just the ones in the index?

app.controller('stories', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {
    var url = 'https://example.firebaseio.com/stories';
    $scope.stories = angularFireCollection(url);
  }
]);
Was it helpful?

Solution

Harry, check out FirebaseIndex for examples on how to use indexes. Almost all the work you do with Firebase is going to involve indexes or normalization of records (matching two sets of records by id), so it's a great topic to study up on.

An index isn't going to work with AngularFire out of the box. However, a little thought and a pull request could be submitted to allow it to work directly with FirebaseIndex (just pass in the FirebaseIndex instead of the url, and in the AngularFire constructor, set this._fref to the FirebaseIndex instead of new Firebase(url).

Example:

function AngularFire($q, $parse, url) {
  this._q = $q;
  this._parse = $parse;
  this._initial = true;
  this._remoteValue = false;
  this._fRef = typeof(url) === 'string'? new Firebase(url) : url;
}

But it's probably not that simple (is anything?). And this would probably be limited to a read-only scope. To make this work in both directions would take quite a bit more planning and leg-work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top