Skip to content

Instantly share code, notes, and snippets.

@harrastia
Forked from zourtney/decorator
Last active August 29, 2015 14:21
Show Gist options
  • Save harrastia/4a963e62605f73bbfff2 to your computer and use it in GitHub Desktop.
Save harrastia/4a963e62605f73bbfff2 to your computer and use it in GitHub Desktop.
Adapted decorator to work in Angular 1.3.8
// Decorator adapted out of Angular 1.3.8
// https://github.com/angular/angular.js/blob/v1.3.8/src/ng/rootScope.js#L196
.config(['$provide', function($provide) {
// Minification-safe hack.
var $$watchers = '$$watchers',
$$nextSibling = '$$nextSibling',
$$childHead = '$$childHead',
$$childTail = '$$childTail',
$$listeners = '$$listeners',
$$listenerCount = '$$listenerCount',
$$ChildScope = '$$childScope',
$id = '$id',
$parent = '$parent',
$$prevSibling = '$$prevSibling',
$root = '$root';
$provide.decorator('$rootScope', ['$delegate', function($rootScope) {
var proto = Object.getPrototypeOf($rootScope);
function nextUid () {
return ++$rootScope.$id;
}
proto.$new = function(isolate, parent) {
var child;
function destroyChild() {
child.$$destroyed = true;
}
parent = parent || this;
if (isolate) {
child = new proto.constructor();
child[$root] = this.$root;
} else {
// Only create a child scope class if somebody asks for one,
// but cache it to allow the VM to optimize lookups.
if (!this.$$ChildScope) {
this[$$ChildScope] = function ChildScope() {
this[$$watchers] = this[$$nextSibling] =
this[$$childHead] = this[$$childTail] = null;
this[$$listeners] = {};
this[$$listenerCount] = {};
this[$id] = nextUid();
this[$$ChildScope] = null;
};
this[$$ChildScope].prototype = this;
}
child = new this[$$ChildScope]();
}
child[$parent] = parent;
child[$$prevSibling] = parent.$$childTail;
if (parent.$$childHead) {
parent[$$childTail][$$nextSibling] = child;
parent[$$childTail] = child;
} else {
parent[$$childHead] = parent[$$childTail] = child;
}
// When the new scope is not isolated or we inherit from `this`, and
// the parent scope is destroyed, the property `$$destroyed` is inherited
// prototypically. In all other cases, this property needs to be set
// when the parent scope is destroyed.
// The listener needs to be added after the parent is set
if (isolate || parent != this) child.$on('$destroy', destroyChild);
return child;
};
$rootScope.$new = proto.$new;
return $rootScope;
}]);
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment