docs(dependency-injection): show providers using map literals

This commit is contained in:
Torgeir Helgevold 2016-05-16 21:56:13 -04:00 committed by Ward Bell
parent 7f1412ca97
commit 1644673d6c
9 changed files with 193 additions and 62 deletions

View File

@ -34,11 +34,16 @@ describe('Dependency Injection Cookbook', function () {
expect(sortedHero).toBeDefined(); expect(sortedHero).toBeDefined();
}); });
it('should render Hero of the Month', function () { it('should render Hero of the Month when DI deps are defined using provide()', function () {
var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month"]')).get(0); var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month"]')).get(0);
expect(heroOfTheMonth).toBeDefined(); expect(heroOfTheMonth).toBeDefined();
}); });
it('should render Hero of the Month when DI deps are defined using provide object literal', function () {
var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month 2"]')).get(0);
expect(heroOfTheMonth).toBeDefined();
});
it('should render Hero Bios', function () { it('should render Hero Bios', function () {
var heroBios = element.all(by.xpath('//h3[text()="Hero Bios"]')).get(0); var heroBios = element.all(by.xpath('//h3[text()="Hero Bios"]')).get(0);
expect(heroBios).toBeDefined(); expect(heroBios).toBeDefined();
@ -54,8 +59,13 @@ describe('Dependency Injection Cookbook', function () {
expect(magmaPhone).toBeDefined(); expect(magmaPhone).toBeDefined();
}); });
it('should render Hero-of-the-Month runner-ups', function () { it('should render Hero-of-the-Month runner-ups when DI deps are defined using provide()', function () {
var runnersUp = element(by.id('rups')).getText(); var runnersUp = element(by.id('rups1')).getText();
expect(runnersUp).toContain('RubberMan, Mr. Nice');
});
it('should render Hero-of-the-Month runner-ups when DI deps are defined using provide object literal', function () {
var runnersUp = element(by.id('rups2')).getText();
expect(runnersUp).toContain('RubberMan, Mr. Nice'); expect(runnersUp).toContain('RubberMan, Mr. Nice');
}); });

View File

@ -23,6 +23,9 @@
<hero-of-the-month></hero-of-the-month> <hero-of-the-month></hero-of-the-month>
</div> </div>
<div class="di-component">
<hero-of-the-month-lit></hero-of-the-month-lit>
</div>
<div class="di-component"> <div class="di-component">
<h3>Unsorted Heroes</h3> <h3>Unsorted Heroes</h3>

View File

@ -9,10 +9,13 @@ import { HeroesBaseComponent,
import { HighlightDirective } from './highlight.directive'; import { HighlightDirective } from './highlight.directive';
import { ParentFinderComponent } from './parent-finder.component'; import { ParentFinderComponent } from './parent-finder.component';
// Object Literal syntax
import { HeroOfTheMonthLiteralsComponent } from './hero-of-the-month-literals.component';
const DIRECTIVES = [ const DIRECTIVES = [
HeroBiosComponent, HeroBiosAndContactsComponent, HeroBiosComponent, HeroBiosAndContactsComponent,
HeroesBaseComponent, SortedHeroesComponent, HeroesBaseComponent, SortedHeroesComponent,
HeroOfTheMonthComponent, HeroOfTheMonthComponent, HeroOfTheMonthLiteralsComponent,
HighlightDirective, HighlightDirective,
ParentFinderComponent ParentFinderComponent
]; ];

View File

@ -0,0 +1,67 @@
/* tslint:disable:one-line:check-open-brace*/
// #docplaster
// #docregion opaque-token
import { OpaqueToken } from '@angular/core';
export const TITLE = new OpaqueToken('title');
// #enddocregion opaque-token
// #docregion hero-of-the-month
import { Component, Inject } from '@angular/core';
import { DateLoggerService,
MinimalLogger } from './date-logger.service';
import { Hero } from './hero';
import { HeroService } from './hero.service';
import { LoggerService } from './logger.service';
import { RUNNERS_UP,
runnersUpFactory } from './runners-up';
// #enddocregion hero-of-the-month
// #docregion some-hero
const someHero = new Hero(42, 'Magma', 'Had a great month!', '555-555-5555');
// #enddocregion some-hero
const template = `
<h3>{{title}}</h3>
<div>Winner: <strong>{{heroOfTheMonth.name}}</strong></div>
<div>Reason for award: <strong>{{heroOfTheMonth.description}}</strong></div>
<div>Runners-up: <strong id="rups2">{{runnersUp}}</strong></div>
<p>Logs:</p>
<div id="logs">
<div *ngFor="let log of logs">{{log}}</div>
</div>
`;
// #docregion hero-of-the-month
@Component({
selector: 'hero-of-the-month-lit',
template: template,
// #docregion providers-using-object-literals
providers: [
{provide: Hero, useValue: someHero},
{provide: TITLE, useValue: 'Hero of the Month - Object Literals'},
{provide: HeroService, useClass: HeroService},
{provide: LoggerService, useClass: DateLoggerService},
{provide: MinimalLogger, useExisting: LoggerService},
{provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService]}
]
// #enddocregion providers-using-object-literals
})
export class HeroOfTheMonthLiteralsComponent {
logs: string[] = [];
// #docregion ctor-signature
constructor(
logger: MinimalLogger,
public heroOfTheMonth: Hero,
@Inject(RUNNERS_UP) public runnersUp: string,
@Inject(TITLE) public title: string)
// #enddocregion ctor-signature
{
this.logs = logger.logs;
logger.logInfo('starting up');
}
}
// #enddocregion hero-of-the-month

View File

@ -26,7 +26,7 @@ const template = `
<h3>{{title}}</h3> <h3>{{title}}</h3>
<div>Winner: <strong>{{heroOfTheMonth.name}}</strong></div> <div>Winner: <strong>{{heroOfTheMonth.name}}</strong></div>
<div>Reason for award: <strong>{{heroOfTheMonth.description}}</strong></div> <div>Reason for award: <strong>{{heroOfTheMonth.description}}</strong></div>
<div>Runners-up: <strong id="rups">{{runnersUp}}</strong></div> <div>Runners-up: <strong id="rups1">{{runnersUp}}</strong></div>
<p>Logs:</p> <p>Logs:</p>
<div id="logs"> <div id="logs">

View File

@ -89,6 +89,11 @@ describe('Dependency Injection Tests', function () {
expect(element(by.css('#p3')).getText()).toEqual(expectedMsg); expect(element(by.css('#p3')).getText()).toEqual(expectedMsg);
}); });
it('P3a (provide) displays as expected', function () {
expectedMsg = 'Hello from logger provided with {provide: Logger, useClass: Logger}';
expect(element(by.css('#p3a')).getText()).toEqual(expectedMsg);
});
it('P4 (useClass:BetterLogger) displays as expected', function () { it('P4 (useClass:BetterLogger) displays as expected', function () {
expectedMsg = 'Hello from logger provided with useClass:BetterLogger'; expectedMsg = 'Hello from logger provided with useClass:BetterLogger';
expect(element(by.css('#p4')).getText()).toEqual(expectedMsg); expect(element(by.css('#p4')).getText()).toEqual(expectedMsg);

View File

@ -1,6 +1,7 @@
/* tslint:disable:one-line:check-open-brace*/
// Examples of provider arrays // Examples of provider arrays
// #docplaster // #docplaster
import { Component, Host, Inject, Injectable, import { Component, Inject, Injectable,
provide, Provider } from '@angular/core'; provide, Provider } from '@angular/core';
import { APP_CONFIG, import { APP_CONFIG,
@ -9,7 +10,7 @@ import { APP_CONFIG,
import { HeroService } from './heroes/hero.service'; import { HeroService } from './heroes/hero.service';
import { heroServiceProvider } from './heroes/hero.service.provider'; import { heroServiceProvider } from './heroes/hero.service.provider';
import { Logger } from './logger.service'; import { Logger } from './logger.service';
import { User, UserService } from './user.service'; import { UserService } from './user.service';
let template = '{{log}}'; let template = '{{log}}';
@ -64,6 +65,23 @@ export class ProviderComponent3 {
} }
} }
//////////////////////////////////////////
@Component({
selector: 'provider-3a',
template: template,
providers:
// #docregion providers-3a
[{provide: Logger, useClass: Logger}]
// #enddocregion providers-3a
})
export class ProviderComponent3a {
log: string;
constructor(logger: Logger) {
logger.log('Hello from logger provided with {provide: Logger, useClass: Logger}');
this.log = logger.logs[0];
}
}
////////////////////////////////////////// //////////////////////////////////////////
class BetterLogger extends Logger {} class BetterLogger extends Logger {}
@ -121,7 +139,7 @@ class NewLogger extends Logger {}
class OldLogger { class OldLogger {
logs: string[] = []; logs: string[] = [];
log(message: string) { log(message: string) {
throw new Error('Should not call the old logger!') throw new Error('Should not call the old logger!');
}; };
} }
@ -175,7 +193,7 @@ export class ProviderComponent6b {
let silentLogger = { let silentLogger = {
logs: ['Silent logger says "Shhhhh!". Provided via "useValue"'], logs: ['Silent logger says "Shhhhh!". Provided via "useValue"'],
log: () => {} log: () => {}
} };
// #enddocregion silent-logger // #enddocregion silent-logger
@Component({ @Component({
@ -206,7 +224,7 @@ export class ProviderComponent8{
// #enddocregion provider-8-ctor // #enddocregion provider-8-ctor
// must be true else this component would have blown up at runtime // must be true else this component would have blown up at runtime
log = 'Hero service injected successfully' log = 'Hero service injected successfully';
} }
///////////////// /////////////////
@ -300,14 +318,14 @@ export class ProviderComponent10b {
this.logger = { this.logger = {
log: (msg: string) => this.logger.logs.push(msg), log: (msg: string) => this.logger.logs.push(msg),
logs: [] logs: []
} };
// #enddocregion provider-10-logger // #enddocregion provider-10-logger
this.logger.log("Optional logger was not available.") this.logger.log('Optional logger was not available.');
// #docregion provider-10-logger // #docregion provider-10-logger
} }
// #enddocregion provider-10-logger // #enddocregion provider-10-logger
else { else {
this.logger.log('Hello from the injected logger.') this.logger.log('Hello from the injected logger.');
this.log = this.logger.logs[0]; this.log = this.logger.logs[0];
} }
this.log = this.logger.logs[0]; this.log = this.logger.logs[0];
@ -322,6 +340,7 @@ export class ProviderComponent10b {
<div id="p1"><provider-1></provider-1></div> <div id="p1"><provider-1></provider-1></div>
<div id="p2"><provider-2></provider-2></div> <div id="p2"><provider-2></provider-2></div>
<div id="p3"><provider-3></provider-3></div> <div id="p3"><provider-3></provider-3></div>
<div id="p3a"><provider-3a></provider-3a></div>
<div id="p4"><provider-4></provider-4></div> <div id="p4"><provider-4></provider-4></div>
<div id="p5"><provider-5></provider-5></div> <div id="p5"><provider-5></provider-5></div>
<div id="p6a"><provider-6a></provider-6a></div> <div id="p6a"><provider-6a></provider-6a></div>
@ -337,6 +356,7 @@ export class ProviderComponent10b {
ProviderComponent1, ProviderComponent1,
ProviderComponent2, ProviderComponent2,
ProviderComponent3, ProviderComponent3,
ProviderComponent3a,
ProviderComponent4, ProviderComponent4,
ProviderComponent5, ProviderComponent5,
ProviderComponent6a, ProviderComponent6a,

View File

@ -29,6 +29,8 @@ include ../_util-fns
* [useExisting - the *alias provider*](#useexisting) * [useExisting - the *alias provider*](#useexisting)
* [useFactory - the *factory provider*](#usefactory) * [useFactory - the *factory provider*](#usefactory)
[Define providers with object literals](#object-literals)
[Provider token alternatives](#tokens) [Provider token alternatives](#tokens)
* [class-interface](#class-interface) * [class-interface](#class-interface)
* [OpaqueToken](#opaque-token) * [OpaqueToken](#opaque-token)
@ -541,6 +543,20 @@ a(id='usefactory')
Look at the [live example](/resources/live-examples/cb-dependency-injection/ts/plnkr.html) Look at the [live example](/resources/live-examples/cb-dependency-injection/ts/plnkr.html)
for the full source code. for the full source code.
<a id="object-literals"></a>
.l-main-section
:marked
## Define providers with object literals
:marked
In the previous section we learned to use the `provide` method to customize how dependencies are created.
Instead of calling the `provide` function, we can configure providers with _object literals_.
It's a slightly more concise syntax and we don't have to import the `provide` function.
Here's a set of providers that we saw earlier, re-written with object literals.
+makeExample('cb-dependency-injection/ts/app/hero-of-the-month-literals.component.ts','providers-using-object-literals')(format='.')
a(id="tokens") a(id="tokens")
.l-main-section .l-main-section

View File

@ -569,14 +569,20 @@ code-example(format, language="html").
// #docregion providers-provide-3 // #docregion providers-provide-3
// Skip for Dart, where the provide() function won't pass type checking. // Skip for Dart, where the provide() function won't pass type checking.
:marked :marked
The [provide](../api/core/provide-function.html) function is the more common, friendlier way to create a `Provider`: The [provide](../api/core/provide-function.html) function is the typical way to create a `Provider`:
// #enddocregion providers-provide-3 // #enddocregion providers-provide-3
+makeExample('dependency-injection/ts/app/providers.component.ts','providers-3') +makeExample('dependency-injection/ts/app/providers.component.ts','providers-3')
// #docregion providers-provide-4-1 // #docregion providers-provide-4-1
// Modified for Dart. // Modified for Dart.
:marked :marked
In both approaches &mdash; `Provider` class and `provide` function &mdash; Or we can declare a provider in an _object literal_ and skip the `provide` function.
we supply two arguments. +makeExample('dependency-injection/ts/app/providers.component.ts','providers-3a')
:marked
Pick the syntax that you prefer. They all do the same thing.
In each syntax, we supply two types of values.
// #enddocregion providers-provide-4-1 // #enddocregion providers-provide-4-1
// #docregion providers-provide-4-2 // #docregion providers-provide-4-2
:marked :marked
@ -589,6 +595,7 @@ code-example(format, language="html").
The second is a provider definition object, The second is a provider definition object,
which we can think of as a *recipe* for creating the dependency value. which we can think of as a *recipe* for creating the dependency value.
There are many ways to create dependency values... and many ways to write a recipe. There are many ways to create dependency values... and many ways to write a recipe.
// #docregion providers-alternative-1 // #docregion providers-alternative-1
:marked :marked
<a id="class-provider"></a> <a id="class-provider"></a>
@ -606,7 +613,7 @@ code-example(format, language="html").
This logger gets the user from the injected `UserService`, This logger gets the user from the injected `UserService`,
which happens also to be injected at the application level. which happens also to be injected at the application level.
// #enddocregion providers-alternative-2 // #enddocregion providers-alternative-2
+makeExample('dependency-injection/ts/app/providers.component.ts','EvenBetterLogger') +makeExample('dependency-injection/ts/app/providers.component.ts','EvenBetterLogger')(format='.')
// #docregion providers-alternative-3 // #docregion providers-alternative-3
:marked :marked
Configure it like we did `BetterLogger`. Configure it like we did `BetterLogger`.