aboutsummaryrefslogtreecommitdiffstats
path: root/test/ScopeSpec.js
diff options
context:
space:
mode:
authorMisko Hevery2010-10-08 17:30:13 -0700
committerMisko Hevery2010-10-12 16:33:06 -0700
commitd9abfe8a7e488be8725f56077527b16f7c79546a (patch)
tree67089c5d2059e7a56afab0fec19dbce76fdab798 /test/ScopeSpec.js
parentfbfd160316de1b99e7afa4102c7fae2ee5b9c1f5 (diff)
downloadangular.js-d9abfe8a7e488be8725f56077527b16f7c79546a.tar.bz2
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject - inject method can return, instance, or call function which have $inject property - initialize services with $creation=[eager|eager-publish] this means that only some of the services are now globally accessible - upgraded $become on scope to use injector hence respect the $inject property for injection - $become should not be run multiple times and will most likely be removed in future version - added $new on scope to create a child scope - $inject is respected on constructor function - simplified scopes so that they no longer have separate __proto__ for parent, api, behavior and instance this should speed up execution since scope will now create one __proto__ chain per scope (not three). BACKWARD COMPATIBILITY WARNING: - services now need to have $inject instead of inject property for proper injection this breaks backward compatibility - not all services are now published into root scope (only: $location, $cookie, $window) - if you have widget/directive which uses services on scope (such as this.$xhr), you will now have to inject that service in (as it is not published on the root scope anymore)
Diffstat (limited to 'test/ScopeSpec.js')
-rw-r--r--test/ScopeSpec.js75
1 files changed, 40 insertions, 35 deletions
diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js
index 66e9d489..4a207bf0 100644
--- a/test/ScopeSpec.js
+++ b/test/ScopeSpec.js
@@ -1,11 +1,27 @@
describe('scope/model', function(){
+ var temp;
+
+ beforeEach(function() {
+ temp = window.temp = {};
+ temp.InjectController = function(exampleService, extra) {
+ this.localService = exampleService;
+ this.extra = extra;
+ this.$root.injectController = this;
+ };
+ temp.InjectController.$inject = ["exampleService"];
+ });
+
+ afterEach(function() {
+ window.temp = undefined;
+ });
+
it('should create a scope with parent', function(){
var model = createScope({name:'Misko'});
expect(model.name).toEqual('Misko');
});
- it('should have $get/set$/parent$', function(){
+ it('should have $get/$set/$parent', function(){
var parent = {};
var model = createScope(parent);
model.$set('name', 'adam');
@@ -138,40 +154,6 @@ describe('scope/model', function(){
});
});
- describe('service injection', function(){
- it('should inject services', function(){
- var scope = createScope(null, {
- service:function(){
- return "ABC";
- }
- });
- expect(scope.service).toEqual("ABC");
- });
-
- it('should inject arugments', function(){
- var scope = createScope(null, {
- name:function(){
- return "misko";
- },
- greet: extend(function(name) {
- return 'hello ' + name;
- }, {inject:['name']})
- });
- expect(scope.greet).toEqual("hello misko");
- });
-
- it('should throw error on missing dependency', function(){
- try {
- createScope(null, {
- greet: extend(function(name) {
- }, {inject:['name']})
- });
- } catch(e) {
- expect(e).toEqual("Don't know how to inject 'name'.");
- }
- });
- });
-
describe('getterFn', function(){
it('should get chain', function(){
expect(getterFn('a.b')(undefined)).toEqual(undefined);
@@ -215,4 +197,27 @@ describe('scope/model', function(){
});
});
+ describe('$new', function(){
+ it('should $new should create new child scope and $become controller', function(){
+ var parent = createScope(null, {exampleService: function(){return 'Example Service';}});
+ var child = parent.$new(temp.InjectController, 10);
+ expect(child.localService).toEqual('Example Service');
+ expect(child.extra).toEqual(10);
+
+ child.$onEval(function(){ this.run = true; });
+ parent.$eval();
+ expect(child.run).toEqual(true);
+ });
+ });
+
+ describe('$become', function(){
+ it('should inject properties on controller defined in $inject', function(){
+ var parent = createScope(null, {exampleService: function(){return 'Example Service';}});
+ var child = createScope(parent);
+ child.$become(temp.InjectController, 10);
+ expect(child.localService).toEqual('Example Service');
+ expect(child.extra).toEqual(10);
+ });
+ });
+
});