style(guide): add more context throughout displaying-data guide

This commit is contained in:
Jeff Cross 2015-05-19 14:33:21 -07:00
parent 20f1992e33
commit 99663edb8c
1 changed files with 50 additions and 45 deletions

View File

@ -110,16 +110,21 @@
code-tabs code-tabs
code-pane(language="javascript" name="TypeScript" format="linenums"). code-pane(language="javascript" name="TypeScript" format="linenums").
//Typescript //Typescript
constructor() { class DisplayComponent {
this.myName = "Alice"; myName: string;
this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; <span class='otl'>names: Array<string>;</span>
constructor() {
this.myName = "Alice";
<span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span>
}
} }
code-pane(language="javascript" name="Javascript (ES5)" format="linenums"). code-pane(language="javascript" name="Javascript (ES5)" format="linenums").
//ES5 //ES5
function DisplayComponent() { function DisplayComponent() {
this.myName = "Alice"; this.myName = "Alice";
this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; <span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span>
} }
p. p.
You can then use this array in your template with the <code>for</code> directive to create copies of DOM elements You can then use this array in your template with the <code>for</code> directive to create copies of DOM elements
@ -155,13 +160,22 @@
code-tabs code-tabs
code-pane(language="javascript" name="TypeScript" format="linenums"). code-pane(language="javascript" name="TypeScript" format="linenums").
//Typescript //Typescript
...
import {Component, View, bootstrap, For} from 'angular2/angular2'; import {Component, View, bootstrap, For} from 'angular2/angular2';
@View({
...
directives: [For] directives: [For]
})
code-pane(language="javascript" name="ES5" format="linenums"). code-pane(language="javascript" name="ES5" format="linenums").
//ES5 //ES5
directives: [angular.For] DisplayComponent.annotations = [
...
new angular.ViewAnnotation({
...
directives: [angular.For]
})
];
p Reload and you've got your list of friends! p Reload and you've got your list of friends!
p. p.
@ -185,39 +199,32 @@
p Using this syntax, you can build UI lists from any iterable object. p Using this syntax, you can build UI lists from any iterable object.
.l-main-section .l-main-section
h2#Create-a-class Create a class for the array property and inject into component h2#Create-a-class Create a class for the array property and inject into component
p. p.
Before we get too much further, we should mention that putting our model (array) directly in our controller isn't Before we get too much further, we should mention that putting our model (array) directly in our controller isn't
proper form. We should separate the concerns by having another class serve the role of model and inject it into proper form. We should separate the concerns by having another class serve the role of model and inject it into
the controller. the controller.
p Make a <code>FriendsService</code> class to provide the model with the list of friends. p Make a <code>FriendsService</code> class to provide the model with the list of friends.
code-example(language="javascript" format="linenums"). code-tabs
class FriendsService { code-pane(language="javascript" name="TypeScript" format="linenums").
names: Array&lt;string&gt;; class FriendsService {
constructor() { names: Array&lt;string&gt;;
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; constructor() {
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
}
}
code-pane(language="javascript" name="ES5" format="linenums").
function FriendsService() {
this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];
} }
}
p. p.
Replace the current list of friends in DisplayComponent by passing in the FriendsService and setting the list of Now replace the current list of friends in DisplayComponent by including the FriendsService in the injectables list,
then including the service in the constructor, and finally setting the list of
names in DisplayComponent to the names provided by the service you passed in. names in DisplayComponent to the names provided by the service you passed in.
code-example(language="javascript" format="linenums").
class DisplayComponent {
names: Array&lt;string&gt;;
constructor(friendsService: FriendsService) {
this.names = friendsService.names;
}
}
p And then make FriendsService available to dependency injection
code-example(language="javascript" format="linenums").
@Component({
...
injectables: [FriendsService]
})
...
class DisplayComponent {...
.callout.is-helpful .callout.is-helpful
header ES5 Note header ES5 Note
p. p.
@ -226,41 +233,39 @@
code-tabs code-tabs
code-pane(language="javascript" name="TypeScript" format="linenums"). code-pane(language="javascript" name="TypeScript" format="linenums").
//TypeScript @Component({
class FriendsService { ...
<span class='otl'>injectables: [FriendsService]</span>
})
class DisplayComponent {
myName: string;
names: Array&lt;string&gt;; names: Array&lt;string&gt;;
constructor() { constructor(<span class='otl'>friendsService: FriendsService</span>) {
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; this.myName = 'Alice';
<span class='otl'>this.names = friendsService.names;</span>
} }
} }
...
class DisplayComponent {
names: Array&lt;string&gt;;
constructor(friendsService: FriendsService) {
this.names = friendsService.names;
}
}
code-pane(language="javascript" name="ES5" format="linenums"). code-pane(language="javascript" name="ES5" format="linenums").
//ES5 //ES5
function FriendsService() { function FriendsService() {
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"]; this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
} }
function DisplayComponent(friends) { function DisplayComponent(<span class='otl'>friends</span>) {
this.myName = "Alice"; this.myName = "Alice";
this.names = friends.names; this.names = <span class='otl'>friends.names</span>;
} }
DisplayComponent.annotations = [ DisplayComponent.annotations = [
new angular.ComponentAnnotation({ new angular.ComponentAnnotation({
selector: "display", selector: "display",
injectables: [FriendsService] <span class='otl'>injectables: [FriendsService]</span>
}), }),
new angular.ViewAnnotation({ new angular.ViewAnnotation({
template: '{{ myName }} &lt;ul&gt; &lt;li *for="#name of names"&gt;{{ name }}&lt;/li&gt; &lt;/ul&gt;', template: '{{ myName }} &lt;ul&gt; &lt;li *for="#name of names"&gt;{{ name }}&lt;/li&gt; &lt;/ul&gt;',
directives: [angular.For, angular.If] directives: [angular.For]
}) })
]; ];
DisplayComponent.parameters = [[FriendsService]]; <span class='otl'>DisplayComponent.parameters = [[FriendsService]];</span>
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
angular.bootstrap(DisplayComponent); angular.bootstrap(DisplayComponent);
}); });