diff options
| author | Igor Minar | 2011-09-01 14:19:22 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-09-01 15:00:22 -0700 |
| commit | 31b86241215bb37cc6bb81f98a47942738d710c2 (patch) | |
| tree | 5cefa3afacad07b28b07654cde2280aa0e51a0eb /src | |
| parent | a5607e3061f5b0aa7988446025dba8f89413fd9d (diff) | |
| download | angular.js-31b86241215bb37cc6bb81f98a47942738d710c2.tar.bz2 | |
feat(scope): add listener deregistration fn for $watch and $on
- both $watch and $on now return a function which when called
deregisters the listener
- $removeListener was removed and replaced with the above
- added more tests for $watch and $on
Closes #542
Diffstat (limited to 'src')
| -rw-r--r-- | src/Scope.js | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/src/Scope.js b/src/Scope.js index e037cb06..6bb9a586 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -257,22 +257,29 @@ Scope.prototype = { * - `string`: Evaluated as {@link guide/dev_guide.expressions expression} * - `function(scope, newValue, oldValue)`: called with current `scope` an previous and * current values as parameters. + * @returns {function()} Returns a deregistration function for this listener. */ $watch: function(watchExp, listener) { - var scope = this; - var get = compileToFn(watchExp, 'watch'); - var listenFn = compileToFn(listener || noop, 'listener'); - var array = scope.$$watchers; + var scope = this, + get = compileToFn(watchExp, 'watch'), + listenFn = compileToFn(listener || noop, 'listener'), + array = scope.$$watchers, + watcher = { + fn: listenFn, + last: Number.NaN, // NaN !== NaN. We used this to force $watch to fire on first run. + get: get + }; + if (!array) { array = scope.$$watchers = []; } // we use unshift since we use a while loop in $digest for speed. // the while loop reads in reverse order. - array.unshift({ - fn: listenFn, - last: Number.NaN, // NaN !== NaN. We used this to force $watch to fire on first run. - get: get - }); + array.unshift(watcher); + + return function() { + angularArray.remove(array, watcher); + }; }, /** @@ -538,6 +545,7 @@ Scope.prototype = { * * @param {string} name Event name to listen on. * @param {function(event)} listener Function to call when the event is emitted. + * @returns {function()} Returns a deregistration function for this listener. * * The event listener function format is: `function(event)`. The `event` object passed into the * listener has the following attributes @@ -553,30 +561,13 @@ Scope.prototype = { this.$$listeners[name] = namedListeners = []; } namedListeners.push(listener); - }, - - /** - * @workInProgress - * @ngdoc function - * @name angular.scope.$removeListener - * @function - * - * @description - * Remove the on listener registered by {@link angular.scope.$on $on}. - * - * @param {string} name Event name to remove on. - * @param {function} listener Function to remove. - */ - $removeListener: function(name, listener) { - var namedListeners = this.$$listeners[name], - i; - if (namedListeners) { - i = indexOf(namedListeners, listener); - namedListeners.splice(i, 1); - } + return function() { + angularArray.remove(namedListeners, listener); + }; }, + /** * @workInProgress * @ngdoc function |
