UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

296 lines 9.11 kB
'use strict';var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var lang_1 = require("angular2/src/facade/lang"); /** * A parameter metadata that specifies a dependency. * * ### Example ([live demo](http://plnkr.co/edit/6uHYJK?p=preview)) * * ```typescript * class Engine {} * * @Injectable() * class Car { * engine; * constructor(@Inject("MyEngine") engine:Engine) { * this.engine = engine; * } * } * * var injector = Injector.resolveAndCreate([ * provide("MyEngine", {useClass: Engine}), * Car * ]); * * expect(injector.get(Car).engine instanceof Engine).toBe(true); * ``` * * When `@Inject()` is not present, {@link Injector} will use the type annotation of the parameter. * * ### Example * * ```typescript * class Engine {} * * @Injectable() * class Car { * constructor(public engine: Engine) {} //same as constructor(@Inject(Engine) engine:Engine) * } * * var injector = Injector.resolveAndCreate([Engine, Car]); * expect(injector.get(Car).engine instanceof Engine).toBe(true); * ``` */ var InjectMetadata = (function () { function InjectMetadata(token) { this.token = token; } InjectMetadata.prototype.toString = function () { return "@Inject(" + lang_1.stringify(this.token) + ")"; }; InjectMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', [Object]) ], InjectMetadata); return InjectMetadata; })(); exports.InjectMetadata = InjectMetadata; /** * A parameter metadata that marks a dependency as optional. {@link Injector} provides `null` if * the dependency is not found. * * ### Example ([live demo](http://plnkr.co/edit/AsryOm?p=preview)) * * ```typescript * class Engine {} * * @Injectable() * class Car { * engine; * constructor(@Optional() engine:Engine) { * this.engine = engine; * } * } * * var injector = Injector.resolveAndCreate([Car]); * expect(injector.get(Car).engine).toBeNull(); * ``` */ var OptionalMetadata = (function () { function OptionalMetadata() { } OptionalMetadata.prototype.toString = function () { return "@Optional()"; }; OptionalMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], OptionalMetadata); return OptionalMetadata; })(); exports.OptionalMetadata = OptionalMetadata; /** * `DependencyMetadata` is used by the framework to extend DI. * This is internal to Angular and should not be used directly. */ var DependencyMetadata = (function () { function DependencyMetadata() { } Object.defineProperty(DependencyMetadata.prototype, "token", { get: function () { return null; }, enumerable: true, configurable: true }); DependencyMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], DependencyMetadata); return DependencyMetadata; })(); exports.DependencyMetadata = DependencyMetadata; /** * A marker metadata that marks a class as available to {@link Injector} for creation. * * ### Example ([live demo](http://plnkr.co/edit/Wk4DMQ?p=preview)) * * ```typescript * @Injectable() * class UsefulService {} * * @Injectable() * class NeedsService { * constructor(public service:UsefulService) {} * } * * var injector = Injector.resolveAndCreate([NeedsService, UsefulService]); * expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true); * ``` * {@link Injector} will throw {@link NoAnnotationError} when trying to instantiate a class that * does not have `@Injectable` marker, as shown in the example below. * * ```typescript * class UsefulService {} * * class NeedsService { * constructor(public service:UsefulService) {} * } * * var injector = Injector.resolveAndCreate([NeedsService, UsefulService]); * expect(() => injector.get(NeedsService)).toThrowError(); * ``` */ var InjectableMetadata = (function () { function InjectableMetadata() { } InjectableMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], InjectableMetadata); return InjectableMetadata; })(); exports.InjectableMetadata = InjectableMetadata; /** * Specifies that an {@link Injector} should retrieve a dependency only from itself. * * ### Example ([live demo](http://plnkr.co/edit/NeagAg?p=preview)) * * ```typescript * class Dependency { * } * * @Injectable() * class NeedsDependency { * dependency; * constructor(@Self() dependency:Dependency) { * this.dependency = dependency; * } * } * * var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]); * var nd = inj.get(NeedsDependency); * * expect(nd.dependency instanceof Dependency).toBe(true); * * var inj = Injector.resolveAndCreate([Dependency]); * var child = inj.resolveAndCreateChild([NeedsDependency]); * expect(() => child.get(NeedsDependency)).toThrowError(); * ``` */ var SelfMetadata = (function () { function SelfMetadata() { } SelfMetadata.prototype.toString = function () { return "@Self()"; }; SelfMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], SelfMetadata); return SelfMetadata; })(); exports.SelfMetadata = SelfMetadata; /** * Specifies that the dependency resolution should start from the parent injector. * * ### Example ([live demo](http://plnkr.co/edit/Wchdzb?p=preview)) * * ```typescript * class Dependency { * } * * @Injectable() * class NeedsDependency { * dependency; * constructor(@SkipSelf() dependency:Dependency) { * this.dependency = dependency; * } * } * * var parent = Injector.resolveAndCreate([Dependency]); * var child = parent.resolveAndCreateChild([NeedsDependency]); * expect(child.get(NeedsDependency).dependency instanceof Depedency).toBe(true); * * var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]); * expect(() => inj.get(NeedsDependency)).toThrowError(); * ``` */ var SkipSelfMetadata = (function () { function SkipSelfMetadata() { } SkipSelfMetadata.prototype.toString = function () { return "@SkipSelf()"; }; SkipSelfMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], SkipSelfMetadata); return SkipSelfMetadata; })(); exports.SkipSelfMetadata = SkipSelfMetadata; /** * Specifies that an injector should retrieve a dependency from any injector until reaching the * closest host. * * In Angular, a component element is automatically declared as a host for all the injectors in * its view. * * ### Example ([live demo](http://plnkr.co/edit/GX79pV?p=preview)) * * In the following example `App` contains `ParentCmp`, which contains `ChildDirective`. * So `ParentCmp` is the host of `ChildDirective`. * * `ChildDirective` depends on two services: `HostService` and `OtherService`. * `HostService` is defined at `ParentCmp`, and `OtherService` is defined at `App`. * *```typescript * class OtherService {} * class HostService {} * * @Directive({ * selector: 'child-directive' * }) * class ChildDirective { * constructor(@Optional() @Host() os:OtherService, @Optional() @Host() hs:HostService){ * console.log("os is null", os); * console.log("hs is NOT null", hs); * } * } * * @Component({ * selector: 'parent-cmp', * providers: [HostService], * template: ` * Dir: <child-directive></child-directive> * `, * directives: [ChildDirective] * }) * class ParentCmp { * } * * @Component({ * selector: 'app', * providers: [OtherService], * template: ` * Parent: <parent-cmp></parent-cmp> * `, * directives: [ParentCmp] * }) * class App { * } * * bootstrap(App); *``` */ var HostMetadata = (function () { function HostMetadata() { } HostMetadata.prototype.toString = function () { return "@Host()"; }; HostMetadata = __decorate([ lang_1.CONST(), __metadata('design:paramtypes', []) ], HostMetadata); return HostMetadata; })(); exports.HostMetadata = HostMetadata; //# sourceMappingURL=metadata.js.map