javascript - angularFire 3 way data binding won't update an function -
i have firebaseobject (myfirebaseservice.getcurrentuser()) bind $scope.user. after binding successful, loop tho object see if object contain "associatedcourseid" equal value ($stateparams.id). if does, $scope.finishlessoncount count up. problem is, when add new object inside firebaseobject (that bindto user) via other page or inside firebase, finishlessoncount value won't change expect 3 way binding. need refresh page see finishlessoncount reflect true value. wrong? want finishlessoncount change using compare function add more finishedlessons firebaseobject. please see code below:
myfirebaseservice.getcurrentuser().$bindto($scope, "user").then(function(){ (var key in $scope.user.finishedlessons) { if ($scope.user.finishedlessons.hasownproperty(key)) { if ($scope.user.finishedlessons[key].associatedcourseid == $stateparams.id) { $scope.finishlessoncount++; } } }; console.log ($scope.finishlessoncount); });
update 1 according @kato solution: decide use extending firebaseoject way solute problem. still, not. did not use factory here simplify thing since need pass in courseid operation. here code:
function countlessons(lessons, courseid) { var count = 0; for(var key in lessons) { if( lessons[key].associatedcourseid == courseid) { count++; } } return count; } var userwithlessonscounter = $firebaseobject.$extend({ $$updated: function(snap) { var changed = $firebaseobject.prototype.$$updated.call(this, snap); this.lessoncount = countlessons(this.finishedlessons, $stateparams.id); } }); var reftemp = new firebase($rootscope.baseurl + "users/" + $rootscope.userid); var usertemp = new userwithlessonscounter(reftemp); usertemp.$bindto($scope, "usertemp").then(function(){ console.log($scope.usertemp); }); usertemp.$watch(function() { console.log("does run @ all? " + $scope.usertemp.lessoncount); });
i update user object, lessoncount value did not change unless refresh page. , console.log inside $watch did not run @ all. wrong?
the promise returned $bindto called once. it's not event listener. can't listen updated each time there change.
please read the guide, start finish, , read angular's $watch method before continuing down route, fundamental knowledge, should not have been first instinct.
a beginner approach utilize $watch:
myfirebaseservice.getcurrentuser().$bindto($scope, "user");
$scope.$watch('user', function() { (var key in $scope.user.finishedlessons) { if ($scope.user.finishedlessons.hasownproperty(key)) { if ($scope.user.finishedlessons[key].associatedcourseid == $stateparams.id) { $scope.finishlessoncount++; } } }; console.log ($scope.finishlessoncount); });
or, having familiarized angularfire api, 1 might pick $scope.user.$watch() in place of scope method, prove more efficient.
having written large portion of angularfire code, pick $extend tool, added precisely use cases this:
// making assumptions here since haven't included // code firebase service, not seem solid app.factory('userwithlessonscounter', function($firebaseobject) { return $firebaseobject.$extend({ $$updated: function(snap) { var changed = $firebaseobject.prototype.$$updated.call(this, snap); this.lessoncount = countlessons(this.finishedlessons); return changed; } }); }); function countlessons(lessons) { var count = 0; for(var key in lessons) { if( lessons.hasownproperty(key) ) { count++; } } return count; }
and in controller:
app.controller('...', function($scope, userwithlessonscounter) { var ref = new firebase(...); var user = new userwithlessoncounter(ref); user.$bindto($scope, 'user'); user.$watch(function() { console.log($scope.user.lessoncount); }); });
Comments
Post a Comment