Skip to content

Commit 115ac5f

Browse files
committed
docs: directive docs
1 parent da4862a commit 115ac5f

File tree

1 file changed

+195
-19
lines changed

1 file changed

+195
-19
lines changed

modules/angular2/src/core/annotations/annotations.js

Lines changed: 195 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,173 @@
11
import {ABSTRACT, CONST, normalizeBlank, isPresent} from 'angular2/src/facade/lang';
22
import {ListWrapper, List} from 'angular2/src/facade/collection';
33

4+
// type StringMap = {[idx: string]: string};
5+
46
/**
5-
* Directives allow you to attach behavior to the DOM elements. Directive is an abstract concept, instead use concrete
6-
* directives such as: [Component], [Decorator] or [Viewport].
7+
* Directives allow you to attach behavior to the DOM elements.
8+
*
9+
* Directive is an abstract concept, instead use concrete directives such as: [Component], [Decorator] or [Viewport].
710
*/
811
@ABSTRACT()
912
export class Directive {
10-
selector:any; //string;
11-
bind:any;
12-
implementsTypes:any; //List;
13-
lifecycle:any; //List
14-
events:any; //List
13+
/**
14+
* The CSS selector that triggers the instantiation of a directive.
15+
*
16+
* Angular only allows directives to trigger on CSS selectors that do not cross element boundaries.
17+
* The supported selectors are:
18+
*
19+
* - `element-name` select by element name.
20+
* - `.class` select by class name.
21+
* - `[attribute]` select by attribute name.
22+
* - `[attribute=value]` select by attribute name and value.
23+
* - `:not(sub_selector)` select only if the element does not match the `sub_selector`.
24+
*
25+
* ## Example
26+
*
27+
* Suppose we have a directive with an `input[type=text]` selector.
28+
*
29+
* And the following HTML:
30+
*
31+
* <form>
32+
* <input type="text">
33+
* <input type="radio">
34+
* <form>
35+
*
36+
* The directive would only be instantiated on the `<input type="text">` element.
37+
*
38+
*/
39+
selector:string;
40+
41+
/**
42+
* Enumerates the set of properties that accept data binding for a directive.
43+
*
44+
* The `bind` property defines a set of `directiveProperty` to `bindingProperty` key-value pairs:
45+
*
46+
* - `directiveProperty` specifies the component property where the value is written.
47+
* - `bindingProperty` specifies the DOM property where the value is read from.
48+
*
49+
* You can include [Pipes] when specifying a `bindingProperty` to allow for data transformation and structural
50+
* change detection of the value.
51+
*
52+
* ## Syntax
53+
* @Directive({
54+
* bind: {
55+
* 'directiveProperty1': 'bindingProperty1',
56+
* 'directiveProperty2': 'bindingProperty2 | pipe1 | ...',
57+
* ...
58+
* }
59+
* }
60+
*
61+
*
62+
* ## Basic Property Binding:
63+
*
64+
* @Decorator({
65+
* selector: '[tooltip]',
66+
* bind: {
67+
* 'tooltipText': 'tooltip'
68+
* }
69+
* })
70+
* class Tooltip {
71+
* set tooltipText(text) {
72+
* // This will get called every time the 'tooltip' binding changes with the new value.
73+
* }
74+
* }
75+
*
76+
* As used in this example:
77+
*
78+
* <div [tooltip]="someExpression">
79+
*
80+
* Whenever the `someExpression` expression changes, the `bind` declaration instructs Angular to update the
81+
* `Tooltip`'s `tooltipText` property.
82+
*
83+
*
84+
* Similarly in this example:
85+
*
86+
* <div tooltip="Some Text">
87+
*
88+
* The `Tooltip`'s `tooltipText` property gets initialized to the `Some Text` literal.
89+
*
90+
*
91+
* ## Bindings With Pipes:
92+
*
93+
* @Decorator({
94+
* selector: '[class-set]',
95+
* bind: {
96+
* 'classChanges': 'classSet | keyValDiff'
97+
* }
98+
* })
99+
* class ClassSet {
100+
* set classChanges(changes:KeyValueChanges) {
101+
* // This will get called every time the `class-set` expressions changes its structure.
102+
* }
103+
* }
104+
*
105+
* As used in this example:
106+
* <div [class-set]="someExpression">
107+
*
108+
* In the above example, the `ClassSet` uses the `keyValDiff` [Pipe] for watching structural changes. This means that
109+
* the `classChanges` setter gets invoked if the expression changes to a different reference, or if the
110+
* structure of the expression changes. (Shallow property watching of the object)
111+
*
112+
* NOTE: The `someExpression` can also contain its own [Pipe]s. In this case, the two pipes compose as if they were
113+
* inlined.
114+
*
115+
*/
116+
bind:any; // StringMap
117+
118+
/**
119+
* Specifies which DOM events the directive listens to and what the action should be.
120+
*
121+
* The `event` property defines a set of `event` to `method` key-value pairs:
122+
*
123+
* - `event` specifies the DOM event that the directive listens to.
124+
* - `onMethod` specifies the method to execute when the event occurs.
125+
*
126+
*
127+
* ## Syntax
128+
* @Directive({
129+
* events: {
130+
* 'event1': 'onMethod',
131+
* ...
132+
* }
133+
* }
134+
*
135+
* ## Basic Event Binding:
136+
*
137+
* @Decorator({
138+
* selector: 'input',
139+
* event: {
140+
* 'change': 'onChange'
141+
* }
142+
* })
143+
* class InputDecorator {
144+
* onChange(event:Event) {
145+
* // invoked whenever the DOM element fires the 'change' event.
146+
* }
147+
* }
148+
*
149+
*/
150+
events:any; // StringMap
151+
152+
/**
153+
* Specifies a set of lifecycle events in which the directive participates.
154+
*/
155+
lifecycle:any; //List<LifecycleEvent>
156+
15157
@CONST()
16158
constructor({
17159
selector,
18160
bind,
19161
events,
20-
implementsTypes,
21162
lifecycle
22163
}:{
23164
selector:string,
24165
bind:any,
25166
events: any,
26-
implementsTypes:List,
27167
lifecycle:List
28168
}={})
29169
{
30170
this.selector = selector;
31-
this.implementsTypes = implementsTypes;
32171
this.bind = bind;
33172
this.events = events;
34173
this.lifecycle = lifecycle;
@@ -49,22 +188,19 @@ export class Component extends Directive {
49188
bind,
50189
events,
51190
services,
52-
implementsTypes,
53191
lifecycle
54192
}:{
55193
selector:String,
56194
bind:Object,
57195
events:Object,
58196
services:List,
59-
implementsTypes:List,
60197
lifecycle:List
61198
}={})
62199
{
63200
super({
64201
selector: selector,
65202
bind: bind,
66203
events: events,
67-
implementsTypes: implementsTypes,
68204
lifecycle: lifecycle
69205
});
70206

@@ -79,14 +215,12 @@ export class Decorator extends Directive {
79215
selector,
80216
bind,
81217
events,
82-
implementsTypes,
83218
lifecycle,
84219
compileChildren = true,
85220
}:{
86221
selector:string,
87222
bind:any,
88223
events:any,
89-
implementsTypes:List,
90224
lifecycle:List,
91225
compileChildren:boolean
92226
}={})
@@ -96,7 +230,6 @@ export class Decorator extends Directive {
96230
selector: selector,
97231
bind: bind,
98232
events: events,
99-
implementsTypes: implementsTypes,
100233
lifecycle: lifecycle
101234
});
102235
}
@@ -108,24 +241,67 @@ export class Viewport extends Directive {
108241
selector,
109242
bind,
110243
events,
111-
implementsTypes,
112244
lifecycle
113245
}:{
114246
selector:string,
115247
bind:any,
116-
implementsTypes:List,
117248
lifecycle:List
118249
}={})
119250
{
120251
super({
121252
selector: selector,
122253
bind: bind,
123254
events: events,
124-
implementsTypes: implementsTypes,
125255
lifecycle: lifecycle
126256
});
127257
}
128258
}
129259

260+
//TODO(misko): turn into LifecycleEvent class once we switch to TypeScript;
261+
262+
/**
263+
* Specify that a directive should be notified whenever a [View] that contains it is destroyed.
264+
*
265+
* ## Example
266+
*
267+
* @Decorator({
268+
* ...,
269+
* lifecycle: [ onDestroy ]
270+
* })
271+
* class ClassSet implements OnDestroy {
272+
* onDestroy() {
273+
* // invoked to notify directive of the containing view destruction.
274+
* }
275+
* }
276+
*/
130277
export const onDestroy = "onDestroy";
278+
279+
280+
/**
281+
* Specify that a directive should be notified when any of its bindings have changed.
282+
*
283+
* ## Example:
284+
*
285+
* @Decorator({
286+
* selector: '[class-set]',
287+
* bind: {
288+
* 'propA': 'propA'
289+
* 'propB': 'propB'
290+
* }
291+
* })
292+
* class ClassSet {
293+
* propA;
294+
* propB;
295+
*
296+
* onChange(changes:{[idx: string, PropertyUpdate]}) {
297+
* // This will get called after any of the properties have been updated.
298+
* if (changes['propA']) {
299+
* // if propA was updated
300+
* }
301+
* if (changes['propA']) {
302+
* // if propB was updated
303+
* }
304+
* }
305+
* }
306+
*/
131307
export const onChange = "onChange";

0 commit comments

Comments
 (0)