Wednesday, September 27, 2017

Angularfire $firebaseArray extend dynamic countsum

Am trying to achieve a dynamic count of certain node if defined:

This works but its not dynamic, you have to call sum().

app.factory("ArrayWithSum", function($firebaseArray) {
  return $firebaseArray.$extend({
    sum: function() {
       var total = 0;
       var todayDate = new Date();
       var start = todayDate.setHours(0,0,0,0);
       var end = todayDate.setHours(23,59,59,999);

        // the array data is located in this.$list
        angular.forEach(this.$list, function(rec) {
            if (angular.isDefined(rec.qa)){
                if (angular.isDefined(rec.qa.completed)) {
                    if (rec.qa.completed >= start && rec.qa.completed <= end){
                        total++;
                    }
                }
            }

        });
        return total;
    }
  });
});

I tried $$update but can’t access this_counter in array:

  app.factory("counter", function($firebaseArray) {
      return $firebaseArray.$extend({
        sum: function() {
            return this._counter;
        },
        $$updated: function(){

           var changed = $firebaseArray.prototype.$$updated.apply(this, arguments);

           var todayDate = new Date();
           var start = todayDate.setHours(0,0,0,0);
           var end = todayDate.setHours(23,59,59,999);

           if( !this._counter ) { 
               this._counter = 0; 
           }

            // the array data is located in this.$list
            angular.forEach(this.$list, function(rec) {
                if (angular.isDefined(rec.qa)){
                    if (angular.isDefined(rec.qa.completed)) {
                        if (rec.qa.completed >= start && rec.qa.completed <= end){
                            this._counter++;
                        }
                    }
                }

            });
          return changed;
        }
      });
    });

Does anyone know how to make a dynamic variable that I can update and access?

Thanks

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/27/angularfire-firebasearray-extend-dynamic-countsum/
via @lzomedia #developer #freelance #web

No comments:

Post a Comment