javascript - Debouncing root scope digest invocations -
if invoke $scope.$apply() ten times in immediate succession presume ten root scope digests occur.
can if call $scope.$apply() debounced trailing call completed final state of application same if debounce not in effect?
can duration of successive root scope digests, given previous digest has completed?
edit:
i clarify purpose of question.
say have controller mycontroller, instance instantiated each instance of directive mydirective.
these controller instances listen event my-event , trigger root-scope digest each time my-event raised.
ten instances of mydirective rendered ui via ng-repeat.
another component raises event my-event. each of ten mycontroller instances trigger root-scope digest.
if put sanity of state of affairs 1 side, question this: if debounce root-scope digest attempts made mycontroller, , ensure trailing attempt gets invoked, correctness of program maintained?
var service = require('my-service'); function mycontroller($scope) { this._$scope = $scope; myservice.on('my-event', this.triggerrootscopedigest.bind(this)); } mycontroller.prototype.triggerrootscopedigest = function() { this._$scope.apply(); }
the edited question still points $applyasync or $evalasync solution.
here's example fiddle comparing both $apply() , $applyasync(): http://jsfiddle.net/635pvkkt/
you'll notice 10 items added via ngrepeat, each watching doapply event, , doapplyasync event, trigger respective functions.
when click button broadcasts doapply, triggers 10 $digest calls, each doing directives work (in case, simple console.log).
the doapplyasync broadcast, however, causes 10 directives work in single $digest.
of course, debounced callback work. pass each directive reference debounced function attached parent controller's scope. if debounce function works correctly , has long enough debounce-time, apply once. in situations that's preferred, original question feels simple enough (assuming triggering $digest main goal of event) substituting $apply $applyasync (or $evalasync, depending on semantics) seems more appropriate.
edit: though results exact same, fiddle more accurate triggers real dom events on elements directly: http://jsfiddle.net/uh9wxxho/
Comments
Post a Comment