Skip to content

Commit a82e208

Browse files
committed
feat(Injector): Support binding to null
1 parent 951a808 commit a82e208

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

modules/angular2/src/di/injector.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
77
import {Key} from './key';
88

99
var _constructing = new Object();
10+
var _notFound = new Object();
1011

1112
class _Waiting {
1213
promise:Promise;
@@ -72,10 +73,10 @@ export class Injector {
7273
var strategy = returnPromise ? this._asyncStrategy : this._syncStrategy;
7374

7475
var instance = strategy.readFromCache(key);
75-
if (isPresent(instance)) return instance;
76+
if (instance !== _notFound) return instance;
7677

7778
instance = strategy.instantiate(key);
78-
if (isPresent(instance)) return instance;
79+
if (instance !== _notFound) return instance;
7980

8081
if (isPresent(this._parent)) {
8182
return this._parent._getByKey(key, returnPromise, returnLazy, optional);
@@ -148,13 +149,13 @@ class _SyncInjectorStrategy {
148149
} else if (isPresent(instance) && !_isWaiting(instance)) {
149150
return instance;
150151
} else {
151-
return null;
152+
return _notFound;
152153
}
153154
}
154155

155156
instantiate(key:Key) {
156157
var binding = this.injector._getBinding(key);
157-
if (isBlank(binding)) return null;
158+
if (isBlank(binding)) return _notFound;
158159

159160
if (binding.providedAsPromise) throw new AsyncBindingError(key);
160161

@@ -198,13 +199,13 @@ class _AsyncInjectorStrategy {
198199
} else if (isPresent(instance)) {
199200
return PromiseWrapper.resolve(instance);
200201
} else {
201-
return null;
202+
return _notFound;
202203
}
203204
}
204205

205206
instantiate(key:Key) {
206207
var binding = this.injector._getBinding(key);
207-
if (isBlank(binding)) return null;
208+
if (isBlank(binding)) return _notFound;
208209

209210
//add a marker so we can detect cyclic dependencies
210211
this.injector._markAsConstructing(key);
@@ -243,7 +244,6 @@ class _AsyncInjectorStrategy {
243244
}
244245
}
245246

246-
247247
function _flattenBindings(bindings:List, res:Map) {
248248
ListWrapper.forEach(bindings, function (b) {
249249
if (b instanceof Binding) {

modules/angular2/test/di/injector_spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ export function main() {
267267
expect(injector.get(Car)).toBeAnInstanceOf(Car);
268268
});
269269

270+
it('should support null values', () => {
271+
var injector = new Injector([bind('null').toValue(null)]);
272+
expect(injector.get('null')).toBe(null);
273+
});
274+
270275
describe("default bindings", function () {
271276
it("should be used when no matching binding found", function () {
272277
var injector = new Injector([], {defaultBindings: true});

0 commit comments

Comments
 (0)