feat(Injector): Support binding to null
This commit is contained in:
		
							parent
							
								
									951a808e0e
								
							
						
					
					
						commit
						a82e20889d
					
				
							
								
								
									
										14
									
								
								modules/angular2/src/di/injector.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										14
									
								
								modules/angular2/src/di/injector.js
									
									
									
									
										vendored
									
									
								
							| @ -7,6 +7,7 @@ import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; | |||||||
| import {Key} from './key'; | import {Key} from './key'; | ||||||
| 
 | 
 | ||||||
| var _constructing = new Object(); | var _constructing = new Object(); | ||||||
|  | var _notFound = new Object(); | ||||||
| 
 | 
 | ||||||
| class _Waiting { | class _Waiting { | ||||||
|   promise:Promise; |   promise:Promise; | ||||||
| @ -72,10 +73,10 @@ export class Injector { | |||||||
|     var strategy = returnPromise ? this._asyncStrategy : this._syncStrategy; |     var strategy = returnPromise ? this._asyncStrategy : this._syncStrategy; | ||||||
| 
 | 
 | ||||||
|     var instance = strategy.readFromCache(key); |     var instance = strategy.readFromCache(key); | ||||||
|     if (isPresent(instance)) return instance; |     if (instance !== _notFound) return instance; | ||||||
| 
 | 
 | ||||||
|     instance = strategy.instantiate(key); |     instance = strategy.instantiate(key); | ||||||
|     if (isPresent(instance)) return instance; |     if (instance !== _notFound) return instance; | ||||||
| 
 | 
 | ||||||
|     if (isPresent(this._parent)) { |     if (isPresent(this._parent)) { | ||||||
|       return this._parent._getByKey(key, returnPromise, returnLazy, optional); |       return this._parent._getByKey(key, returnPromise, returnLazy, optional); | ||||||
| @ -148,13 +149,13 @@ class _SyncInjectorStrategy { | |||||||
|     } else if (isPresent(instance) && !_isWaiting(instance)) { |     } else if (isPresent(instance) && !_isWaiting(instance)) { | ||||||
|       return instance; |       return instance; | ||||||
|     } else { |     } else { | ||||||
|       return null; |       return _notFound; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   instantiate(key:Key) { |   instantiate(key:Key) { | ||||||
|     var binding = this.injector._getBinding(key); |     var binding = this.injector._getBinding(key); | ||||||
|     if (isBlank(binding)) return null; |     if (isBlank(binding)) return _notFound; | ||||||
| 
 | 
 | ||||||
|     if (binding.providedAsPromise) throw new AsyncBindingError(key); |     if (binding.providedAsPromise) throw new AsyncBindingError(key); | ||||||
| 
 | 
 | ||||||
| @ -198,13 +199,13 @@ class _AsyncInjectorStrategy { | |||||||
|     } else if (isPresent(instance)) { |     } else if (isPresent(instance)) { | ||||||
|       return PromiseWrapper.resolve(instance); |       return PromiseWrapper.resolve(instance); | ||||||
|     } else { |     } else { | ||||||
|       return null; |       return _notFound; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   instantiate(key:Key) { |   instantiate(key:Key) { | ||||||
|     var binding = this.injector._getBinding(key); |     var binding = this.injector._getBinding(key); | ||||||
|     if (isBlank(binding)) return null; |     if (isBlank(binding)) return _notFound; | ||||||
| 
 | 
 | ||||||
|     //add a marker so we can detect cyclic dependencies
 |     //add a marker so we can detect cyclic dependencies
 | ||||||
|     this.injector._markAsConstructing(key); |     this.injector._markAsConstructing(key); | ||||||
| @ -243,7 +244,6 @@ class _AsyncInjectorStrategy { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| function _flattenBindings(bindings:List, res:Map) { | function _flattenBindings(bindings:List, res:Map) { | ||||||
|   ListWrapper.forEach(bindings, function (b) { |   ListWrapper.forEach(bindings, function (b) { | ||||||
|     if (b instanceof Binding) { |     if (b instanceof Binding) { | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								modules/angular2/test/di/injector_spec.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								modules/angular2/test/di/injector_spec.js
									
									
									
									
										vendored
									
									
								
							| @ -267,6 +267,11 @@ export function main() { | |||||||
|       expect(injector.get(Car)).toBeAnInstanceOf(Car); |       expect(injector.get(Car)).toBeAnInstanceOf(Car); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|  |     it('should support null values', () => { | ||||||
|  |       var injector = new Injector([bind('null').toValue(null)]); | ||||||
|  |       expect(injector.get('null')).toBe(null); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|     describe("default bindings", function () { |     describe("default bindings", function () { | ||||||
|       it("should be used when no matching binding found", function () { |       it("should be used when no matching binding found", function () { | ||||||
|         var injector = new Injector([], {defaultBindings: true}); |         var injector = new Injector([], {defaultBindings: true}); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user