From 6f8276a8e3735396999bd158005ca86bb1bb0978 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 22 Mar 2010 16:07:42 -0700 Subject: ng-watch directive --- src/Scope.js | 4 ++++ src/directives.js | 22 +++++++++++----------- test/directivesSpec.js | 12 ++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Scope.js b/src/Scope.js index d22604fd..7b1d2673 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -207,6 +207,10 @@ Scope.prototype = { }, addWatchListener: function(watchExpression, listener) { + // TODO: clean me up! + if (!isFunction(listener)) { + listener = bind(this, this.compile(listener), {scope: this, self: this.state}); + } var watcher = this.watchListeners[watchExpression]; if (!watcher) { watcher = {listeners:[], expression:watchExpression}; diff --git a/src/directives.js b/src/directives.js index 861805fe..e081d179 100644 --- a/src/directives.js +++ b/src/directives.js @@ -82,6 +82,17 @@ angularDirective("ng-repeat", function(expression, element){ }; }, {exclusive: true}); +angularDirective("ng-watch", function(expression, element){ + var match = expression.match(/^([^.]*):(.*)$/); + if (!match) { + throw "Expecting watch expression 'ident_to_watch: watch_statement' got '" + + expression + "'"; + } + return function(){ + this.$watch(match[1], match[2]); + }; +}); + ///////////////////////////////////////// ///////////////////////////////////////// @@ -109,17 +120,6 @@ angularDirective("action", function(expression, element){ }; }); -//ng-watch -//
-angularDirective("watch", function(expression, element){ - var watches = { - 'lhs':'rhs' - }; // parse - return function(){ - this.$watch(watches); - }; -}); - //widget related //ng-validate, ng-required, ng-formatter //ng-error diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 2cee20d1..e0e53eeb 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -82,4 +82,16 @@ describe("directives", function(){ }); expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;"); }); + + it('should ng-watch', function(){ + var scope = compile('