Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($rootScope): allow $watch to be triggered by an event #14798

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ function $RootScopeProvider() {
return ChildScope;
}

function watchEquals(value, last, objectEquality) {
return value !== last &&
!(objectEquality
? equals(value, last)
: (typeof value === 'number' && typeof last === 'number'
&& isNaN(value) && isNaN(last)))
}

this.$get = ['$exceptionHandler', '$parse', '$browser',
function($exceptionHandler, $parse, $browser) {

Expand Down Expand Up @@ -384,6 +392,29 @@ function $RootScopeProvider() {
* @returns {function()} Returns a deregistration function for this listener.
*/
$watch: function(watchExp, listener, objectEquality, prettyPrintExpression) {
if (!angular.isFunction(listener)) {
listener = angular.noop;
}

if (typeof watchExp === 'string' && watchExp.match(/^ *[a-zA-Z]+ *:/)) {
var statement = watchExp.split(':'),
eventName = statement[0].trim(),
get = $parse(statement.slice(1).join(':')),
last = initWatchVal,
scope = this;
function watchOnEvaluate() {
var reallyLast,
value = get(scope);
if (watchEquals(value, last, objectEquality)) {
reallyLast = last; // before setting new last
last = objectEquality ? copy(value, null) : value;
return listener(value, (reallyLast === initWatchVal ? value : reallyLast), scope);
}
}
watchOnEvaluate();
return this.$on(eventName, watchOnEvaluate);
}

var get = $parse(watchExp);

if (get.$$watchDelegate) {
Expand All @@ -401,10 +432,6 @@ function $RootScopeProvider() {

lastDirtyWatch = null;

if (!isFunction(listener)) {
watcher.fn = noop;
}

if (!array) {
array = scope.$$watchers = [];
}
Expand Down Expand Up @@ -795,11 +822,7 @@ function $RootScopeProvider() {
// circuit it with === operator, only when === fails do we use .equals
if (watch) {
get = watch.get;
if ((value = get(current)) !== (last = watch.last) &&
!(watch.eq
? equals(value, last)
: (typeof value === 'number' && typeof last === 'number'
&& isNaN(value) && isNaN(last)))) {
if (watchEquals((value = get(current)), (last = watch.last), watch.eq)) {
dirty = true;
lastDirtyWatch = watch;
watch.last = watch.eq ? copy(value, null) : value;
Expand Down