Skip to content

Commit 23b255a

Browse files
committed
remove $init on scope from applying compilation template
Closes angular#40
1 parent e2154cb commit 23b255a

12 files changed

Lines changed: 95 additions & 108 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<a name="0.9.12"><a/>
22
# <angular/> 0.9.12 thought-implanter (in-progress) #
33

4+
### Breaking changes
5+
- Removed the $init() method after the compilation. The old way of compiling the DOM element was
6+
angular.compile(element).$init(); The $init was there to allow the users to do any work to the
7+
scope before the view would be bound. This is a left over from not having proper MVC. The new
8+
recommended way to deal with initializing scope is to put it in the root constructor controller.
9+
To migrate simply remove the call to $init() and move any code you had before $init() to the
10+
root controller.
11+
412

513
<a name="0.9.11"><a/>
614
# <angular/> 0.9.11 snow-maker (2011-02-08) #

src/Angular.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,8 @@ function merge(src, dst) {
800800
* Compiles a piece of HTML or DOM into a {@link angular.scope scope} object.
801801
<pre>
802802
var scope1 = angular.compile(window.document);
803-
scope1.$init();
804803
805804
var scope2 = angular.compile('<div ng:click="clicked = true">click me</div>');
806-
scope2.$init();
807805
</pre>
808806
*
809807
* @param {string|DOMElement} element Element to compile.
@@ -948,7 +946,7 @@ function toKeyValue(obj) {
948946
(function(window, previousOnLoad){
949947
window.onload = function(){
950948
try { (previousOnLoad||angular.noop)(); } catch(e) {}
951-
angular.compile(window.document).$init();
949+
angular.compile(window.document);
952950
};
953951
})(window, window.onload);
954952
&lt;/script&gt;
@@ -1002,8 +1000,6 @@ function angularInit(config){
10021000
$browser.addCss(config.base_url + config.css);
10031001
else if(msie<8)
10041002
$browser.addJs(config.base_url + config.ie_compat, config.ie_compat_id);
1005-
1006-
scope.$init();
10071003
}
10081004
}
10091005

src/Compiler.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Template(priority) {
1313
}
1414

1515
Template.prototype = {
16-
init: function(element, scope) {
16+
attach: function(element, scope) {
1717
var inits = {};
1818
this.collectInits(element, inits, scope);
1919
forEachSorted(inits, function(queue){
@@ -96,18 +96,14 @@ Compiler.prototype = {
9696
template = this.templatize(element, index, 0) || new Template();
9797
return function(element, parentScope){
9898
element = jqLite(element);
99-
var scope = parentScope && parentScope.$eval ?
100-
parentScope : createScope(parentScope);
99+
var scope = parentScope && parentScope.$eval
100+
? parentScope
101+
: createScope(parentScope);
101102
element.data($$scope, scope);
102-
return extend(scope, {
103-
$element:element,
104-
$init: function() {
105-
template.init(element, scope);
106-
scope.$eval();
107-
delete scope.$init;
108-
return scope;
109-
}
110-
});
103+
template.attach(element, scope);
104+
scope.$element = element;
105+
scope.$eval();
106+
return scope;
111107
};
112108
},
113109

src/widgets.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@ angularWidget('ng:include', function(element){
677677
element.html(response);
678678
childScope = useScope || createScope(scope);
679679
compiler.compile(element)(element, childScope);
680-
childScope.$init();
681680
scope.$eval(onloadExp);
682681
});
683682
} else {
@@ -795,7 +794,6 @@ var ngSwitch = angularWidget('ng:switch', function (element){
795794
element.append(caseElement);
796795
childScope.$tryEval(switchCase.change, element);
797796
switchCase.template(caseElement, childScope);
798-
childScope.$init();
799797
}
800798
});
801799
});
@@ -891,7 +889,7 @@ angularWidget('a', function() {
891889
angularWidget("@ng:repeat", function(expression, element){
892890
element.removeAttr('ng:repeat');
893891
element.replaceWith(jqLite("<!-- ng:repeat: " + expression + " --!>"));
894-
var template = this.compile(element);
892+
var linker = this.compile(element);
895893
return function(reference){
896894
var match = expression.match(/^\s*(.+)\s+in\s+(.*)\s*$/),
897895
lhs, rhs, valueIdent, keyIdent;
@@ -912,6 +910,7 @@ angularWidget("@ng:repeat", function(expression, element){
912910
var children = [], currentScope = this;
913911
this.$onEval(function(){
914912
var index = 0,
913+
cloneElement,
915914
childCount = children.length,
916915
lastElement = reference,
917916
collection = this.$tryEval(rhs, reference),
@@ -937,16 +936,17 @@ angularWidget("@ng:repeat", function(expression, element){
937936
if (keyIdent) childScope[keyIdent] = key;
938937
} else {
939938
// grow children
940-
childScope = template(quickClone(element), createScope(currentScope));
939+
childScope = createScope(currentScope);
941940
childScope[valueIdent] = collection[key];
942941
if (keyIdent) childScope[keyIdent] = key;
943-
lastElement.after(childScope.$element);
944942
childScope.$index = index;
945943
childScope.$position = index == 0 ?
946-
'first' :
947-
(index == collectionLength - 1 ? 'last' : 'middle');
948-
childScope.$element.attr('ng:repeat-index', index);
949-
childScope.$init();
944+
'first' :
945+
(index == collectionLength - 1 ? 'last' : 'middle');
946+
cloneElement = quickClone(element);
947+
lastElement.after(cloneElement);
948+
cloneElement.attr('ng:repeat-index', index);
949+
linker(cloneElement, childScope);
950950
children.push(childScope);
951951
}
952952
childScope.$eval();
@@ -1069,7 +1069,6 @@ angularWidget('ng:view', function(element) {
10691069
$xhr('GET', src, function(code, response){
10701070
element.html(response);
10711071
compiler.compile(element)(element, childScope);
1072-
childScope.$init();
10731072
});
10741073
} else {
10751074
element.html('');

test/BinderSpec.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ describe('Binder', function(){
33
beforeEach(function(){
44
var self = this;
55

6-
this.compile = function(html, initialScope, parent) {
6+
this.compile = function(html, parent) {
77
var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
88
if (self.element) dealoc(self.element);
9-
var element = self.element = jqLite(html);
9+
var element;
10+
if (parent) {
11+
parent.html(html);
12+
element = parent.children();
13+
} else {
14+
element = jqLite(html);
15+
}
16+
self.element = element;
1017
var scope = compiler.compile(element)(element);
11-
12-
if (parent) parent.append(element);
13-
14-
extend(scope, initialScope);
15-
scope.$init();
1618
return {node:element, scope:scope};
1719
};
1820
this.compileToHtml = function (content) {
@@ -27,8 +29,8 @@ describe('Binder', function(){
2729
});
2830

2931

30-
it('ChangingTextfieldUpdatesModel', function(){
31-
var state = this.compile('<input type="text" name="model.price" value="abc">', {model:{}});
32+
it('text-field should default to value attribute', function(){
33+
var state = this.compile('<input type="text" name="model.price" value="abc">');
3234
state.scope.$eval();
3335
assertEquals('abc', state.scope.model.price);
3436
});
@@ -443,8 +445,7 @@ describe('Binder', function(){
443445
});
444446

445447
it('ActionOnAHrefThrowsError', function(){
446-
var model = {books:[]};
447-
var c = this.compile('<a ng:click="action()">Add Phone</a>', model);
448+
var c = this.compile('<a ng:click="action()">Add Phone</a>');
448449
c.scope.action = function(){
449450
throw new Error('MyError');
450451
};
@@ -517,9 +518,9 @@ describe('Binder', function(){
517518
});
518519

519520
it('ValidateForm', function(){
520-
var c = this.compile('<div><input name="name" ng:required>' +
521-
'<div ng:repeat="item in items"><input name="item.name" ng:required/></div></div>',
522-
undefined, jqLite(document.body));
521+
var c = this.compile('<div id="test"><input name="name" ng:required>' +
522+
'<input ng:repeat="item in items" name="item.name" ng:required/></div>',
523+
jqLite(document.body));
523524
var items = [{}, {}];
524525
c.scope.$set("items", items);
525526
c.scope.$eval();
@@ -547,7 +548,7 @@ describe('Binder', function(){
547548
});
548549

549550
it('ValidateOnlyVisibleItems', function(){
550-
var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', undefined, jqLite(document.body));
551+
var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', jqLite(document.body));
551552
c.scope.$set("show", true);
552553
c.scope.$eval();
553554
assertEquals(2, c.scope.$service('$invalidWidgets').length);

test/CompilerSpec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('compiler', function(){
2828
compile = function(html){
2929
var e = jqLite("<div>" + html + "</div>");
3030
var scope = compiler.compile(e)(e);
31-
scope.$init();
3231
return scope;
3332
};
3433
});
@@ -48,10 +47,8 @@ describe('compiler', function(){
4847
};
4948
};
5049
var template = compiler.compile(e);
51-
scope = template(e);
52-
var init = scope.$init;
5350
expect(log).toEqual("found");
54-
init();
51+
scope = template(e);
5552
expect(e.hasClass('ng-directive')).toEqual(true);
5653
expect(log).toEqual("found:init");
5754
});

test/ScenarioSpec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@ describe("ScenarioSpec: Compilation", function(){
1313
it("should compile dom node and return scope", function(){
1414
var node = jqLite('<div ng:init="a=1">{{b=a+1}}</div>')[0];
1515
scope = compile(node);
16-
scope.$init();
1716
expect(scope.a).toEqual(1);
1817
expect(scope.b).toEqual(2);
1918
});
2019

2120
it("should compile jQuery node and return scope", function(){
22-
scope = compile(jqLite('<div>{{a=123}}</div>')).$init();
21+
scope = compile(jqLite('<div>{{a=123}}</div>'));
2322
expect(jqLite(scope.$element).text()).toEqual('123');
2423
});
2524

2625
it("should compile text node and return scope", function(){
27-
scope = compile('<div>{{a=123}}</div>').$init();
26+
scope = compile('<div>{{a=123}}</div>');
2827
expect(jqLite(scope.$element).text()).toEqual('123');
2928
});
3029
});
3130

3231
describe('scope', function(){
33-
it("should have set, get, eval, $init, updateView methods", function(){
34-
scope = compile('<div>{{a}}</div>').$init();
32+
it("should have $set, $get, $eval, $updateView methods", function(){
33+
scope = compile('<div>{{a}}</div>');
3534
scope.$eval("$invalidWidgets.push({})");
3635
expect(scope.$set("a", 2)).toEqual(2);
3736
expect(scope.$get("a")).toEqual(2);

test/ValidatorsSpec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('ValidatorTest', function(){
99
};
1010
var scope = compile('<input name="name" ng:validate="myValidator:\'hevery\'"/>');
1111
scope.name = 'misko';
12-
scope.$init();
12+
scope.$eval();
1313
assertEquals('misko', validator.first);
1414
assertEquals('hevery', validator.last);
1515
expect(validator._this.$id).toEqual(scope.$id);
@@ -99,7 +99,6 @@ describe('ValidatorTest', function(){
9999
jqLite(document.body).append(self.$element);
100100
self.$element.data('$validate', noop);
101101
self.$root = self;
102-
self.$init();
103102
});
104103

105104
afterEach(function(){
@@ -110,7 +109,6 @@ describe('ValidatorTest', function(){
110109
var value, fn;
111110
var scope = compile('<input type="text" name="name" ng:validate="asynchronous:asyncFn"/>');
112111
jqLite(document.body).append(scope.$element);
113-
scope.$init();
114112
var input = scope.$element;
115113
scope.asyncFn = function(v,f){
116114
value=v; fn=f;
@@ -155,7 +153,6 @@ describe('ValidatorTest', function(){
155153
scope.asyncFn = jasmine.createSpy();
156154
scope.updateFn = jasmine.createSpy();
157155
scope.name = 'misko';
158-
scope.$init();
159156
scope.$eval();
160157
expect(scope.asyncFn).wasCalledWith('misko', scope.asyncFn.mostRecentCall.args[1]);
161158
assertTrue(scope.$element.hasClass('ng-input-indicator-wait'));

test/directivesSpec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ describe("directive", function(){
77
compile = function(html) {
88
element = jqLite(html);
99
model = compiler.compile(element)(element);
10-
model.$init();
1110
return model;
1211
};
1312
});
@@ -116,7 +115,7 @@ describe("directive", function(){
116115
expect(element.attr('src')).toEqual('http://localhost/mysrc');
117116
expect(element.attr('alt')).toEqual('myalt');
118117
});
119-
118+
120119
it('should not pretty print JSON in attributes', function(){
121120
var scope = compile('<img alt="{{ {a:1} }}"/>');
122121
expect(element.attr('alt')).toEqual('{"a":1}');

test/markupSpec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe("markups", function(){
99
compile = function(html) {
1010
element = jqLite(html);
1111
scope = compiler.compile(element)(element);
12-
scope.$init();
1312
};
1413
});
1514

0 commit comments

Comments
 (0)