diff options
| author | Misko Hevery | 2010-03-31 17:56:16 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-03-31 18:18:10 -0700 |
| commit | 11a6431f8926c557f3c58408dacc98466e76cde1 (patch) | |
| tree | ab36304fd373d0947ca36c577e25ca87a1c894af /src/Scope.js | |
| parent | 35a91085004e31f786df1e0011bc26ed0142ab4d (diff) | |
| download | angular.js-11a6431f8926c557f3c58408dacc98466e76cde1.tar.bz2 | |
started to add services
Diffstat (limited to 'src/Scope.js')
| -rw-r--r-- | src/Scope.js | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/src/Scope.js b/src/Scope.js index 4144d456..ba86e24f 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -89,7 +89,11 @@ function createScope(parent, Class) { function API(){} function Behavior(){} - var instance, behavior, api, watchList = [], evalList = []; + var instance, behavior, api, evalLists = {}; + if (isFunction(parent)) { + Class = parent; + parent = {}; + } Class = Class || noop; parent = Parent.prototype = parent || {}; @@ -107,15 +111,10 @@ function createScope(parent, Class) { if (isDefined(exp)) { return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length)); } else { - foreach(watchList, function(watch) { - var value = instance.$tryEval(watch.watch, watch.handler); - if (watch.last !== value) { - instance.$tryEval(watch.listener, watch.handler, value, watch.last); - watch.last = value; - } - }); - foreach(evalList, function(eval) { - instance.$tryEval(eval.fn, eval.handler); + foreachSorted(evalLists, function(list) { + foreach(list, function(eval) { + instance.$tryEval(eval.fn, eval.handler); + }); }); } }, @@ -134,16 +133,24 @@ function createScope(parent, Class) { }, $watch: function(watchExp, listener, exceptionHandler) { - var watch = expressionCompile(watchExp); - watchList.push({ - watch: watch, - last: watch.call(instance), - handler: exceptionHandler, - listener:expressionCompile(listener) + var watch = expressionCompile(watchExp), + last = watch.call(instance); + instance.$onEval(PRIORITY_WATCH, function(){ + var value = watch.call(instance); + if (last !== value) { + instance.$tryEval(listener, exceptionHandler, value, last); + last = value; + } }); }, - $onEval: function(expr, exceptionHandler){ + $onEval: function(priority, expr, exceptionHandler){ + if (!isNumber(priority)) { + exceptionHandler = expr; + expr = priority; + priority = 0; + } + var evalList = evalLists[priority] || (evalLists[priority] = []); evalList.push({ fn: expressionCompile(expr), handler: exceptionHandler @@ -151,7 +158,21 @@ function createScope(parent, Class) { } }); + if (isUndefined(instance.$root)) { + behavior.$root = instance; + behavior.$parent = instance; + } + Class.apply(instance, slice.call(arguments, 2, arguments.length)); return instance; } + +function serviceAdapter(services) { + return function(){ + var self = this; + foreach(services, function(service, name){ + self[name] = service.call(self); + }); + }; +}; |
