Skip to content

Commit 1a1c61b

Browse files
committed
fix: Apply mixins to rootScope
1 parent 084e78a commit 1a1c61b

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

dist/angular-meteor.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@
15951595
angular.module(name, [])
15961596

15971597
/*
1598-
A service which lets us apply mixins into the `ChildScope` prototype.
1598+
A service which lets us apply mixins into Scope.prototype.
15991599
The flow is simple. Once we define a mixin, it will be stored in the `$Mixer`,
16001600
and any time a `ChildScope` prototype is created
16011601
it will be extended by the `$Mixer`.
@@ -1608,6 +1608,9 @@
16081608
var _this = this;
16091609

16101610
this._mixins = [];
1611+
// Apply mixins automatically on specified contexts
1612+
this._autoExtend = [];
1613+
this._autoConstruct = [];
16111614

16121615
// Adds a new mixin
16131616
this.mixin = function (mixin) {
@@ -1616,6 +1619,13 @@
16161619
}
16171620

16181621
_this._mixins = _.union(_this._mixins, [mixin]);
1622+
// Apply mixins to stored contexts
1623+
_this._autoExtend.forEach(function (context) {
1624+
return _this._extend(context);
1625+
});
1626+
_this._autoConstruct.forEach(function (context) {
1627+
return _this._construct(context);
1628+
});
16191629
return _this;
16201630
};
16211631

@@ -1665,24 +1675,15 @@
16651675
var Scope = $rootScope.constructor;
16661676
var $new = $rootScope.$new;
16671677

1668-
// Extends and constructs every newly created scope without affecting the root scope
1669-
Scope.prototype.$new = function (isolate, parent) {
1670-
var firstChild = this === $rootScope && !this.$$ChildScope;
1671-
var scope = $new.call(this, isolate, parent);
1672-
1673-
// If the scope is isolated we would like to extend it aswell
1674-
if (isolate) {
1675-
// The scope is the prototype of its upcomming child scopes, so the methods would
1676-
// be accessable to them as well
1677-
$Mixer._extend(scope);
1678-
}
1679-
// Else, if this is the first child of the root scope we would like to apply the extensions
1680-
// without affection the root scope
1681-
else if (firstChild) {
1682-
// Creating a middle layer where all the extensions are gonna be applied to
1683-
scope.__proto__ = this.$$ChildScope.prototype = $Mixer._extend(Object.create(this));
1684-
}
1678+
// Apply extensions whether a mixin in defined.
1679+
// Note that this way mixins which are initialized later
1680+
// can be applied on rootScope.
1681+
$Mixer._autoExtend.push(Scope.prototype);
1682+
$Mixer._autoConstruct.push($rootScope);
16851683

1684+
Scope.prototype.$new = function () {
1685+
var scope = $new.apply(this, arguments);
1686+
// Apply constructors to newly created scopes
16861687
return $Mixer._construct(scope);
16871688
};
16881689
}]);

src/modules/mixer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const Mixer = '$Mixer';
44
angular.module(name, [])
55

66
/*
7-
A service which lets us apply mixins into the `ChildScope` prototype.
7+
A service which lets us apply mixins into Scope.prototype.
88
The flow is simple. Once we define a mixin, it will be stored in the `$Mixer`,
99
and any time a `ChildScope` prototype is created
1010
it will be extended by the `$Mixer`.
@@ -15,6 +15,9 @@ angular.module(name, [])
1515
*/
1616
.service(Mixer, function() {
1717
this._mixins = [];
18+
// Apply mixins automatically on specified contexts
19+
this._autoExtend = [];
20+
this._autoConstruct = [];
1821

1922
// Adds a new mixin
2023
this.mixin = (mixin) => {
@@ -23,6 +26,9 @@ angular.module(name, [])
2326
}
2427

2528
this._mixins = _.union(this._mixins, [mixin]);
29+
// Apply mixins to stored contexts
30+
this._autoExtend.forEach(context => this._extend(context));
31+
this._autoConstruct.forEach(context => this._construct(context));
2632
return this;
2733
};
2834

src/modules/scope.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,15 @@ angular.module(name, [
1313
const Scope = $rootScope.constructor;
1414
const $new = $rootScope.$new;
1515

16-
// Extends and constructs every newly created scope without affecting the root scope
17-
Scope.prototype.$new = function(isolate, parent) {
18-
const firstChild = this === $rootScope && !this.$$ChildScope;
19-
const scope = $new.call(this, isolate, parent);
20-
21-
// If the scope is isolated we would like to extend it aswell
22-
if (isolate) {
23-
// The scope is the prototype of its upcomming child scopes, so the methods would
24-
// be accessable to them as well
25-
$Mixer._extend(scope);
26-
}
27-
// Else, if this is the first child of the root scope we would like to apply the extensions
28-
// without affection the root scope
29-
else if (firstChild) {
30-
// Creating a middle layer where all the extensions are gonna be applied to
31-
scope.__proto__ = this.$$ChildScope.prototype =
32-
$Mixer._extend(Object.create(this));
33-
}
16+
// Apply extensions whether a mixin in defined.
17+
// Note that this way mixins which are initialized later
18+
// can be applied on rootScope.
19+
$Mixer._autoExtend.push(Scope.prototype);
20+
$Mixer._autoConstruct.push($rootScope);
3421

22+
Scope.prototype.$new = function() {
23+
const scope = $new.apply(this, arguments);
24+
// Apply constructors to newly created scopes
3525
return $Mixer._construct(scope);
3626
};
3727
}

tests/integration/mixer.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ describe('angular-meteor.mixer', function() {
2424
});
2525

2626
afterEach(function() {
27-
delete $rootScope.$$ChildScope;
2827
$Mixer._mixout(Mixin);
2928
});
3029

31-
it('should leave root scope as is', function() {
32-
expect($rootScope.prop).toBeUndefined();
33-
expect($rootScope.$method).toBeUndefined();
30+
it('should extend root scope', function() {
31+
expect($rootScope.prop).toEqual('prop');
32+
expect($rootScope.$method).toEqual(jasmine.any(Function));
3433
});
3534

3635
it('should extend child scope', function() {

0 commit comments

Comments
 (0)