From e6460685869e16b5016de975fd0ba15a7e436951 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 5 Apr 2010 21:26:52 -0700 Subject: added ng-controller directive --- src/Scope.js | 14 ++++++++++++-- src/directives.js | 11 +++++++++++ src/jqLite.js | 3 ++- test/directivesSpec.js | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Scope.js b/src/Scope.js index 562dfbd8..b41f7436 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -1,4 +1,4 @@ -function getter(instance, path) { +function getter(instance, path, unboundFn) { if (!path) return instance; var element = path.split('.'); var key; @@ -22,7 +22,7 @@ function getter(instance, path) { } } } - if (typeof instance === 'function' && !instance['$$factory']) { + if (!unboundFn && isFunction(instance) && !instance['$$factory']) { return bind(lastInstance, instance); } return instance; @@ -146,7 +146,17 @@ function createScope(parent, services, existing) { fn: expressionCompile(expr), handler: exceptionHandler }); + }, + + $become: function(Class) { + // remove existing + foreach(behavior, function(value, key){ delete behavior[key]; }); + foreach((Class || noop).prototype, function(fn, name){ + behavior[name] = bind(instance, fn); + }); + (Class || noop).call(instance); } + }); if (!parent.$root) { diff --git a/src/directives.js b/src/directives.js index 4f2916da..d1b1dba3 100644 --- a/src/directives.js +++ b/src/directives.js @@ -4,6 +4,17 @@ angularDirective("ng-init", function(expression){ }; }); +angularDirective("ng-controller", function(expression){ + return function(element){ + var controller = getter(window, expression, true) || getter(this, expression, true); + if (!controller) + throw "Can not find '"+expression+"' controller."; + if (!isFunction(controller)) + throw "Reference '"+expression+"' is not a class."; + this.$become(controller); + }; +}); + angularDirective("ng-eval", function(expression){ return function(element){ this.$onEval(expression, element); diff --git a/src/jqLite.js b/src/jqLite.js index 331db68d..9aa4f2c7 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -115,7 +115,8 @@ JQLite.prototype = { remove: function() { this.dealoc(); - this[0].parentNode.removeChild(this[0]); + var parentNode = this[0].parentNode; + if (parentNode) parentNode.removeChild(this[0]); }, removeAttr: function(name) { diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 06a3a2dd..0a7e3c18 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -156,4 +156,19 @@ describe("directives", function(){ scope.$eval(); expect(element.css('display')).toEqual(''); }); + + it('should ng-controller', function(){ + window.Greeter = function(){ + this.greeting = 'hello'; + }; + window.Greeter.prototype = { + greet: function(name) { + return this.greeting + ' ' + name; + } + }; + var scope = compile('
'); + expect(scope.greeting).toEqual('hello'); + expect(scope.greet('misko')).toEqual('hello misko'); + delete window.Greeter; + }); }); -- cgit v1.2.3