1
votes

I've been struggling with the proper implementation of this, I thought I had it but based on the example I am doing it wrong. How do you use this factory in a controller? What is the intended result?

The example:

app.factory("ListWithTotal", ["$FirebaseArray", "$firebase", function($FirebaseArray, $firebase) {
  // create a new factory based on $FirebaseArray
  var TotalFactory = $FirebaseArray.$extendFactory({
    getTotal: function() {
      var total = 0;
      // the array data is located in this.$list
      angular.forEach(this.$list, function(rec) {
        total += rec.amount;
      });
      return total;
    }
  });
  return function(listRef) {
    // override the factory used by $firebase
    **// why do listRef and ref not match here?
    //where does listRef or ref come from**
    var sync = $firebase(ref, {arrayFactory: TotalFactory});
    return sync.$asArray(); // this will be an instance of TotalFactory
  }
}]);

I've been able to use a version of extending factories that doesn't include the return of a reference to a new firebase. For example: A todo list that keeps count: http://plnkr.co/edit/iAGvPHFWn2GSPGzBRpKh?p=preview

1
It's a typo. They should both be listRef, or ref. Not one and the other. - user2943490

1 Answers

1
votes

It's a typo. They should both be listRef, or ref. Not one and the other.

As the ListWithTotal factory returns a function, you use it in the controller like this:

.controller('MyController', function ($scope, $window, ListWithTotal) {
    var ref = new $window.Firebase("//some-url/data");
    $scope.listWithTotal = ListWithTotal(ref);
});