Skip to content

Commit 95c3270

Browse files
committed
directives
1 parent 82a6680 commit 95c3270

File tree

6 files changed

+130
-68
lines changed

6 files changed

+130
-68
lines changed

source/api/directives.md

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ type: api
33
order: 6
44
---
55

6-
## Data Binding Directives
6+
## Reactive Directives
77

8-
> These directives can bind themselves to a property on the ViewModel, or to an expression which is evaluated in the context of the ViewModel. When the value of the underlying property or expression changes, the `update()` function of these directives will be called asynchronously on next tick.
8+
> These directives can bind themselves to a property on the Vue instance, or to an expression which is evaluated in the context of the instance. When the value of the underlying property or expression changes, the `update()` function of these directives will be called asynchronously on next tick.
99
1010
### v-text
1111

@@ -35,9 +35,9 @@ If a directive argument is provided, the argument will be the class to be toggle
3535

3636
``` html
3737
<span v-class="
38-
red : hasError,
39-
bold : isImportant,
40-
hidden : isHidden
38+
red : hasError,
39+
bold : isImportant,
40+
hidden : isHidden
4141
"></span>
4242
```
4343

@@ -71,9 +71,9 @@ When there is an argument, it will be used as the CSS property to apply. Combine
7171

7272
``` html
7373
<div v-style="
74-
top: top + 'px',
75-
left: left + 'px',
76-
background-color: 'rgb(0,0,' + bg + ')'
74+
top: top + 'px',
75+
left: left + 'px',
76+
background-color: 'rgb(0,0,' + bg + ')'
7777
"></div>
7878
```
7979

@@ -88,29 +88,48 @@ When the argument is prefixed with `$`, Vue.js will automatically apply prefixed
8888
### v-on
8989

9090
- This directive requires an argument.
91-
- This directive requires the value to be a Function or an expression.
91+
- This directive requires the value to be a Function or a statement.
9292

9393
Attaches an event listener to the element. The event type is denoted by the argument. It is also the only directive that can be used with the `key` filter. For more details see [Listening for Events](/guide/events.html).
9494

9595
### v-model
9696

97-
- This directive can only be used on INPUT, SELECT, TEXTAREA and elements with `contenteditable` attribute.
97+
- This directive can only be used on `<input>`, `<select>` or `<textarea>` elements.
98+
- Directive params: `lazy`, `number`, `options`
9899

99-
Create a two-way binding on a form or editable element. Data is synced on every `input` event by default. When the ViewModel has the `lazy` option set to true, data will be synced only on `change` events. For more details see [Handling Forms](/guide/forms.html).
100+
Create a two-way binding on a form input element. Data is synced on every `input` event by default. For detailed examples see [Handling Forms](/guide/forms.html).
100101

101102
### v-if
102103

103-
- This directive creates child ViewModels.
104104
- This directive can trigger transitions.
105105

106-
Conditionally insert / remove the element based on the truthy-ness of the binding value. A child ViewModel will be created on the bound element. It is instantiated when the value is truthy, and destroyed when the value becomes falsy. If the binding starts with a falsy value, the ViewModel will not be instantiated until the value actually becomes truthy.
106+
Conditionally insert / remove the element based on the truthy-ness of the binding value. If the element is a `<template>` element, its content will be extracted as the conditional block.
107+
108+
**Example:**
109+
110+
``` html
111+
<template v-if="test">
112+
<p>hello</p>
113+
<p>world</p>
114+
</template>
115+
```
116+
117+
Will render:
118+
119+
``` html
120+
<!--v-if-start-->
121+
<p>hello</p>
122+
<p>world</p>
123+
<!--v-if-end-->
124+
```
107125

108126
### v-repeat
109127

110-
- This directive creates child ViewModels.
128+
- This directive creates child Vue instances.
111129
- This directive requires the value to be an Array or Object.
112130
- This directive can trigger transitions.
113131
- This directive accepts an optional argument.
132+
- Directive params: `trackby`
114133

115134
Create a child ViewModel for every item in the binding Array. These child ViewModels will be automatically created / destroyed when mutating methods, e.g. `push()`, are called on the Array.
116135

@@ -120,64 +139,59 @@ When no argument is provided, the child ViewModel will directly use the assigned
120139

121140
``` html
122141
<ul>
123-
<li v-repeat="users">
124-
{&#123;name&#125;} {&#123;email&#125;}
125-
</li>
142+
<li v-repeat="users">
143+
{&#123;name&#125;} {&#123;email&#125;}
144+
</li>
126145
</ul>
127146
```
128147

129148
If an argument is provided, a wrapper data object will always be created, using the argument string as the alias key. This allows for more explicit property access in templates:
130149

131150
``` html
132151
<ul>
133-
<li v-repeat="user : users">
134-
{&#123;user.name&#125;} {&#123;user.email&#125;}
135-
</li>
152+
<li v-repeat="user : users">
153+
{&#123;user.name&#125;} {&#123;user.email&#125;}
154+
</li>
136155
</ul>
137156
```
138157

139158
For detailed examples, see [Displaying a List](/guide/list.html).
140159

141-
### v-view
142-
143-
- This directive creates child ViewModels.
144-
- This directive can trigger transitions.
145-
146-
Conditionally instantiate ViewModels, using the bound value as the Component ID to look up constructors with. When the bound value changes, existing ViewModel will be destroyed and a new ViewModel will be created. When a ViewModel is created, the original element will be replaced, but all attributes will be copied to the new element. For more details, see [Routing](/guide/application.html#Routing).
147-
148160
### v-with
149161

150-
- This directive must be used with another directive that creates child ViewModels.
162+
- This directive can only be used with `v-component`.
151163
- This directive accepts only keypaths, no expressions.
152164

153-
Allows a child ViewModel to inherit data from the parents. You can either pass in an Object which will be used as the `data` option, or bind individual parent properties to the child with different keys. This directive must be used in combination with another directive that creates a child ViewModel, e.g. `v-component` or `v-view`.
165+
Allows a child ViewModel to inherit data from the parents. You can either pass in an Object which will be used as the `data` option, or bind individual parent properties to the child with different keys. This directive must be used in combination with `v-component`.
166+
167+
The data inheritance is one-way: when parent property changes, the child will be notified of the change and update accordingly; however if the child sets the property to something else, it will not affect the parent, and the modified property will be overwritten the next time parent property changes.
154168

155169
Example inheriting an object:
156170

157171
``` js
158172
// parent data looks like this
159173
{
160-
user: {
161-
name: 'Foo Bar',
162-
email: 'foo@bar.com'
163-
}
174+
user: {
175+
name: 'Foo Bar',
176+
email: 'foo@bar.com'
177+
}
164178
}
165179
```
166180

167181
``` html
168-
<div v-with="user">
169-
<!-- you can access properties without `user.` -->
170-
{&#123;name&#125;} {&#123;email&#125;}
171-
</div>
182+
<my-component v-with="user">
183+
<!-- you can access properties without `user.` -->
184+
{&#123;name&#125;} {&#123;email&#125;}
185+
</my-component>
172186
```
173187

174188
Example inheriting individual properties (using the same data):
175189

176190
```
177-
<div v-with="myName : user.name, myEmail: user.email">
178-
<!-- you can access properties with the new keys -->
179-
{&#123;myName&#125;} {&#123;myEmail&#125;}
180-
</div>
191+
<my-component v-with="myName: user.name, myEmail: user.email">
192+
<!-- you can access properties with the new keys -->
193+
{&#123;myName&#125;} {&#123;myEmail&#125;}
194+
</my-component>
181195
```
182196

183197
## Literal Directives
@@ -186,41 +200,46 @@ Example inheriting individual properties (using the same data):
186200
187201
### v-component
188202

203+
- Directive params: `keep-alive`
204+
189205
Compile this element as a child ViewModel with a registered component constructor. This can be used with `v-with` to inehrit data from the parent. For more details see [Component System](/guide/components.html).
190206

191207
### v-ref
192208

193-
Only respected when used in combination with directives that create child components: `v-component`, `v-view`, `v-if` or `v-repeat`. The ViewModel will be accessible in its parent's `$` object, e.g. `parent.$[id]`. When used with `v-repeat`, the value will be an Array containing the child ViewModel instances corresponding to the Array they are bound to. For examples see [Accessing Child Components](/guide/components.html#Accessing_Child_Components).
209+
Register a reference to a child component on its parent for easier access. Only respected when used in combination with `v-component` or `v-repeat`. The component instance will be accessible on its parent's `$` object. For an example, see [child reference](/guide/components.html#Child_Reference).
194210

195-
### v-partial
211+
When used with `v-repeat`, the value will be an Array containing all the child Vue instances corresponding to the Array they are bound to.
196212

197-
Replace the element's innerHTML with a registered partial. Partials can be registered with `Vue.partial()` or passed inside the `partials` option. You can also use this syntax (which doesn't support expressions):
213+
### v-el
198214

199-
``` html
200-
<div>&#123;&#123;> my-partial&#125;&#125;</div>
201-
```
215+
Register a reference to a DOM element on its owner Vue instance for easier access. e.g. `<div v-el="hi">` will be accessible as `vm.$$.hi`.
202216

203-
### v-effect
217+
### v-partial
204218

205-
Apply a registered JavaScript effect to the element. JavaScript effects can be registered with `Vue.effect()` or passed inside the `effects` option.
219+
Replace the element's innerHTML with a registered partial. Partials can be registered with `Vue.partial()` or passed inside the `partials` option.
206220

207-
For details, see [the guide](/guide/transitions.html#JavaScript_Functions).
221+
Using the mustache tag inside `v-partial` makes it reactive:
208222

209-
## Empty Directives
223+
``` html
224+
<!-- content will change based on vm.partialId -->
225+
<div v-partial="{&#123;partialId&#125;}"></div>
226+
```
210227

211-
> Empty directives do not require and will ignore their attribute value.
228+
You can also use this syntax (which doesn't support reactivity):
212229

213-
### v-transition
230+
``` html
231+
<div>&#123;&#123;> my-partial&#125;&#125;</div>
232+
```
214233

215-
Notify Vue.js to apply transition CSS classes to this element. The transition classes are applied when certain transition-triggering directives modify the element, or when the ViewModel's DOM manipulation methods are called.
234+
### v-transition
216235

217-
For details, see [the guide](/guide/transitions.html#CSS_Transitions).
236+
Notify Vue.js to apply transitions to this element. The transition classes are applied when certain transition-triggering directives modify the element, or when the Vue instance's DOM manipulation methods are called.
218237

219-
### v-animation
238+
For details, see [the guide on transitions](/guide/transitions.html).
220239

221-
Notify Vue.js to apply animation CSS classes to this element. The order of CSS class application is different from `v-transition`.
240+
## Empty Directives
222241

223-
For details, see [the guide](/guide/transitions.html#CSS_Animations).
242+
> Empty directives do not require and will ignore their attribute value.
224243
225244
### v-pre
226245

source/api/global-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Here are the list of all the avaiable settings with their default values:
2727
silent: false,
2828
// interpolate mustache bindings?
2929
interpolate: true,
30-
// use async rendering?
30+
// use async updates (for directives & watchers)?
3131
async: true,
3232
// allow altering observed Array's prototype chain?
3333
proto: true

source/api/options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ parent.a // -> 2
292292

293293
### events
294294

295-
An object where keys are events to listen for and values are the corresponding callbacks. The value can also be a string of a method name. The Vue instance will call `$on()` for each entry in the object at instantiation.
295+
An object where keys are events to listen for and values are the corresponding callbacks. Note these are Vue events rather than DOM events. The value can also be a string of a method name. The Vue instance will call `$on()` for each entry in the object at instantiation.
296296

297297
**Example:**
298298

source/guide/components.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ It is important to understand the difference between `Vue.extend()` and `Vue.com
6262

6363
Vue.js supports two different API paradigms: the class-based, imperative, Backbone style API, and the markup-based, declarative, Web Components style API. If you are confused, think about how you can create an image element with `new Image()`, or with an `<img>` tag. Each is useful in its own right and Vue.js provides both for maximum flexibility.
6464

65+
## Dynamic Components
66+
67+
You can dynamically switch between components by using Mustache tags inside the `v-component` direcitve, which can be used together with routers to achieve "page switching":
68+
69+
``` js
70+
new Vue({
71+
el: 'body',
72+
data: {
73+
currentView: 'home'
74+
},
75+
components: {
76+
home: { /* ... */ },
77+
posts: { /* ... */ },
78+
archive: { /* ... */ }
79+
}
80+
})
81+
```
82+
83+
``` html
84+
<div v-component="{&#123;currentView&#125;}">
85+
<!-- content changes when vm.currentview changes! -->
86+
</div>
87+
```
88+
89+
If you want to keep the switched-out components alive so that you can preserve its state or avoid re-rendering, you can add a `keep-alive` directive param:
90+
91+
``` html
92+
<div v-component="{&#123;currentView&#125;}" keep-alive>
93+
<!-- inactive components will be cached! -->
94+
</div>
95+
```
96+
6597
## Component Lifecycle
6698

6799
Every component, or Vue instance, has its own lifecycle: it will be created, compiled, inserted or detached, and finally destroyed. At each of these key moments the instance will emit corresponding events, and when creating an instance or defining a component, we can pass in lifecycle hook functions to react to these events. For example:

source/guide/forms.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ new Vue({
7777
})
7878
</script>
7979

80+
## Lazy Updates
81+
82+
By default, `v-model` syncs the input with the data after each `input` event. You can add a `lazy` attribute to change the behavior to sync after `change` events:
83+
84+
``` html
85+
<!-- synced after "change" instead of "input" -->
86+
<input v-model="msg" lazy>
87+
```
88+
89+
## Casting Value as Number
90+
91+
If you want user input to be automatically persisted as numbers, you can add a `number` attribute to your `v-model` managed inputs:
92+
93+
``` html
94+
<input v-model="age" number>
95+
```
96+
8097
## Dynamic Select Options
8198

8299
When you need to dynamically render a list of options for a `<select>` element, it's recommended to use an `options` attribute together with `v-model`:
@@ -129,12 +146,4 @@ Will render:
129146
</select>
130147
```
131148

132-
## Casting Value as Number
133-
134-
If you want user input to be automatically persisted as numbers, you can add a `number` attribute to your `v-model` managed inputs:
135-
136-
``` html
137-
<input v-model="age" number>
138-
```
139-
140149
Next: [Computed Properties](/guide/computed.html).

source/guide/list.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ demo.items = demo.items.filter(function (item) {
155155

156156
You might think this will blow away the existing DOM and re-build everything. But worry not - Vue.js recognizes array elements that already have an associated Vue instance and will reuse those instances whenever possible.
157157

158+
## Using `trackby`
159+
158160
In some cases, you might need to replace the Array with completely new objects - e.g. ones returned from an API call. If your data objects have a unique id property, then you can use a `trackby` attribute to give Vue.js a hint so that it can reuse an existing instance with data that has the same id.
159161

160162
For example, if your data looks like this:

0 commit comments

Comments
 (0)