0

Update for a better question:

I was able to get the factory running properly and have it total a number out of the loop. Now I'm having trouble getting that number to show up in the view and having that number show up every time a new item is added/removed/updated in the list.

The "ListWithTotal" factory works exactly like in the documentation. I wasn't properly setting the ref variable.

Here's the latest relevant code:

//controller
$scope.total = TodoService.getTotalPoints();

//service
var todos = $firebase(ref, {arrayFactory: "ListWithTotal"}).$asArray();

  return {
    getTotalPoints:function(){
         todos.$loaded().then(function() {
        total = todos.getTotal();
        console.log(total);
        return total;
      });
    },

I can't get this to return total to the view or update when the points value change. Can anyone please provide some direction?

1
  • For a more simple question was is the .x in rec.x? Commented Sep 11, 2014 at 23:58

1 Answer 1

3

The AngularFire guide covers this exact example in the section on extending factories.

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
    var sync = $firebase(ref, {arrayFactory: TotalFactory});
    return sync.$asArray(); // this will be an instance of TotalFactory
  }
}]);
Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand how to use this. Can you explain any further please?
Not in less paragraphs than the guide can cover it.
I guess the main part I don't understand is how to connect this to my array in the todoService? How do I run the getTotal method? Thanks for any help you can offer. I read the doc but still don't understand. I'm new to this.
You've already connected it and invoked it in your example when you called todos.sum() on about line 10 of your code. Exactly what isn't working? Exactly what's unclear in the docs?
I don't understand how to connect the todos array with this new factory. I have var todos = $firebase(ref, {arrayFactory: "ListWithTotal"}).$asArray(); in my todoService. I'm getting an error: ref is not defined pointing to var sync = $firebase(ref, {arrayFactory: TotalFactory}); in the ListWithTotal factory. When I try to define ref I get other errors like: Error: Must pass a valid Firebase reference to $firebase (not a string or URL)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.