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

Commit 6417a3e

Browse files
feat(Scope): allow the parent of a new scope to be specified on creation
This enables us to place transclude scopes more accurately in the scope hierarchy.
1 parent 07e3abc commit 6417a3e

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/ng/rootScope.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,20 @@ function $RootScopeProvider(){
184184
* When creating widgets, it is useful for the widget to not accidentally read parent
185185
* state.
186186
*
187+
* @param {Scope} [parent=this] The {@link ng.$rootScope.Scope `Scope`} that will be the `$parent`
188+
* of the newly created scope. Defaults to `this` scope if not provided.
189+
* This is used when creating a transclude scope to correctly place it
190+
* in the scope hierarchy while maintaining the correct prototypical
191+
* inheritance.
192+
*
187193
* @returns {Object} The newly created child scope.
188194
*
189195
*/
190-
$new: function(isolate) {
196+
$new: function(isolate, parent) {
191197
var child;
192198

199+
parent = parent || this;
200+
193201
if (isolate) {
194202
child = new Scope();
195203
child.$root = this.$root;
@@ -213,13 +221,13 @@ function $RootScopeProvider(){
213221
child = new this.$$ChildScope();
214222
}
215223
child['this'] = child;
216-
child.$parent = this;
217-
child.$$prevSibling = this.$$childTail;
218-
if (this.$$childHead) {
219-
this.$$childTail.$$nextSibling = child;
220-
this.$$childTail = child;
224+
child.$parent = parent;
225+
child.$$prevSibling = parent.$$childTail;
226+
if (parent.$$childHead) {
227+
parent.$$childTail.$$nextSibling = child;
228+
parent.$$childTail = child;
221229
} else {
222-
this.$$childHead = this.$$childTail = child;
230+
parent.$$childHead = parent.$$childTail = child;
223231
}
224232
return child;
225233
},

test/ng/rootScopeSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ describe('Scope', function() {
7272
expect(child.$new).toBe($rootScope.$new);
7373
expect(child.$root).toBe($rootScope);
7474
}));
75+
76+
it("should attach the child scope to a specified parent", inject(function($rootScope) {
77+
var isolated = $rootScope.$new(true);
78+
var trans = $rootScope.$new(false, isolated);
79+
$rootScope.a = 123;
80+
expect(isolated.a).toBeUndefined();
81+
expect(trans.a).toEqual(123);
82+
expect(trans.$parent).toBe(isolated);
83+
}));
7584
});
7685

7786

0 commit comments

Comments
 (0)