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

Commit e646068

Browse files
committed
added ng-controller directive
1 parent 2107eaf commit e646068

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Scope.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getter(instance, path) {
1+
function getter(instance, path, unboundFn) {
22
if (!path) return instance;
33
var element = path.split('.');
44
var key;
@@ -22,7 +22,7 @@ function getter(instance, path) {
2222
}
2323
}
2424
}
25-
if (typeof instance === 'function' && !instance['$$factory']) {
25+
if (!unboundFn && isFunction(instance) && !instance['$$factory']) {
2626
return bind(lastInstance, instance);
2727
}
2828
return instance;
@@ -146,7 +146,17 @@ function createScope(parent, services, existing) {
146146
fn: expressionCompile(expr),
147147
handler: exceptionHandler
148148
});
149+
},
150+
151+
$become: function(Class) {
152+
// remove existing
153+
foreach(behavior, function(value, key){ delete behavior[key]; });
154+
foreach((Class || noop).prototype, function(fn, name){
155+
behavior[name] = bind(instance, fn);
156+
});
157+
(Class || noop).call(instance);
149158
}
159+
150160
});
151161

152162
if (!parent.$root) {

src/directives.js

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ angularDirective("ng-init", function(expression){
44
};
55
});
66

7+
angularDirective("ng-controller", function(expression){
8+
return function(element){
9+
var controller = getter(window, expression, true) || getter(this, expression, true);
10+
if (!controller)
11+
throw "Can not find '"+expression+"' controller.";
12+
if (!isFunction(controller))
13+
throw "Reference '"+expression+"' is not a class.";
14+
this.$become(controller);
15+
};
16+
});
17+
718
angularDirective("ng-eval", function(expression){
819
return function(element){
920
this.$onEval(expression, element);

src/jqLite.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ JQLite.prototype = {
115115

116116
remove: function() {
117117
this.dealoc();
118-
this[0].parentNode.removeChild(this[0]);
118+
var parentNode = this[0].parentNode;
119+
if (parentNode) parentNode.removeChild(this[0]);
119120
},
120121

121122
removeAttr: function(name) {

test/directivesSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,19 @@ describe("directives", function(){
156156
scope.$eval();
157157
expect(element.css('display')).toEqual('');
158158
});
159+
160+
it('should ng-controller', function(){
161+
window.Greeter = function(){
162+
this.greeting = 'hello';
163+
};
164+
window.Greeter.prototype = {
165+
greet: function(name) {
166+
return this.greeting + ' ' + name;
167+
}
168+
};
169+
var scope = compile('<div ng-controller="Greeter"></div>');
170+
expect(scope.greeting).toEqual('hello');
171+
expect(scope.greet('misko')).toEqual('hello misko');
172+
delete window.Greeter;
173+
});
159174
});

0 commit comments

Comments
 (0)