Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 309ec34

Browse files
committed
feat($injector) better locals passing
- $locals service added - passes locals to instantiated service
1 parent 2d5297e commit 309ec34

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/auto/injector.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ function createInjector(modulesToLoad) {
416416
})),
417417
instanceCache = {},
418418
instanceInjector = (instanceCache.$injector =
419-
createInternalInjector(instanceCache, function(servicename) {
419+
createInternalInjector(instanceCache, function(servicename, locals) {
420420
var provider = providerInjector.get(servicename + providerSuffix);
421-
return instanceInjector.invoke(provider.$get, provider);
421+
return instanceInjector.invoke(provider.$get, provider, locals);
422422
}));
423423

424424

@@ -453,8 +453,8 @@ function createInjector(modulesToLoad) {
453453
function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); }
454454

455455
function service(name, constructor) {
456-
return factory(name, ['$injector', function($injector) {
457-
return $injector.instantiate(constructor);
456+
return factory(name, ['$injector', '$locals', function($injector, $locals) {
457+
return $injector.instantiate(constructor, $locals);
458458
}]);
459459
}
460460

@@ -525,7 +525,7 @@ function createInjector(modulesToLoad) {
525525

526526
function createInternalInjector(cache, factory) {
527527

528-
function getService(serviceName) {
528+
function getService(serviceName, locals) {
529529
if (typeof serviceName !== 'string') {
530530
throw Error('Service name expected');
531531
}
@@ -538,7 +538,7 @@ function createInjector(modulesToLoad) {
538538
try {
539539
path.unshift(serviceName);
540540
cache[serviceName] = INSTANTIATING;
541-
return cache[serviceName] = factory(serviceName);
541+
return cache[serviceName] = factory(serviceName, locals);
542542
} finally {
543543
path.shift();
544544
}
@@ -549,15 +549,17 @@ function createInjector(modulesToLoad) {
549549
var args = [],
550550
$inject = annotate(fn),
551551
length, i,
552-
key;
552+
key, val;
553553

554554
for(i = 0, length = $inject.length; i < length; i++) {
555555
key = $inject[i];
556-
args.push(
557-
locals && locals.hasOwnProperty(key)
558-
? locals[key]
559-
: getService(key)
560-
);
556+
if (locals && locals.hasOwnProperty(key))
557+
val = locals[key];
558+
else if (key === '$locals')
559+
val = locals;
560+
else
561+
val = getService(key, locals);
562+
args.push(val);
561563
}
562564
if (!fn.$inject) {
563565
// this means that we must be an array.

test/auto/injectorSpec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ describe('injector', function() {
123123
injector.invoke(['a', 123], {});
124124
}).toThrow("Argument 'fn' is not a function, got number");
125125
});
126+
127+
it('should provide locals as service', function() {
128+
fn.$inject = ['a', 'b', 'c', '$locals'];
129+
injector.invoke(fn, {name: "this"}, {c:3});
130+
expect(args).toEqual([{name: 'this'}, 1, 2, 3, {c: 3}]);
131+
});
126132
});
127133

128134

@@ -369,6 +375,19 @@ describe('injector', function() {
369375
expect(injector.get('foo') instanceof Type).toBe(true);
370376
expect(injector.get('bar') instanceof Type).toBe(true);
371377
});
378+
379+
380+
it('should receive locals', function() {
381+
var Type = function(value) {
382+
this.value = value;
383+
};
384+
385+
var instance = createInjector([function($provide) {
386+
$provide.service('foo', Type);
387+
}]).get('foo', {value: 123});
388+
389+
expect(instance.value).toBe(123);
390+
});
372391
});
373392

374393

test/ng/controllerSpec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
describe('$controller', function() {
4-
var $controllerProvider, $controller;
4+
var $controllerProvider, $controller, $provide;
55

6-
beforeEach(module(function(_$controllerProvider_) {
6+
beforeEach(module(function(_$controllerProvider_, _$provide_) {
77
$controllerProvider = _$controllerProvider_;
8+
$provide = _$provide_;
89
}));
910

1011

@@ -57,6 +58,7 @@ describe('$controller', function() {
5758
expect(scope.foo).toBe('bar');
5859
expect(ctrl instanceof FooCtrl).toBe(true);
5960
});
61+
6062
});
6163

6264

@@ -99,4 +101,14 @@ describe('$controller', function() {
99101
expect(scope.foo).toBe(foo);
100102
expect(scope.foo.mark).toBe('foo');
101103
});
104+
105+
it('should give services current scope', function() {
106+
$provide.service('bar', ['$scope', function($scope) {$scope.bar = 123; }])
107+
108+
var Foo = function($scope, bar) {};
109+
110+
var scope = {};
111+
var ctrl = $controller(Foo, {$scope: scope});
112+
expect(scope.bar).toBe(123);
113+
});
102114
});

0 commit comments

Comments
 (0)