Skip to content

Commit c99de87

Browse files
committed
0.8.0 + computed
1 parent 6f2a45f commit c99de87

File tree

8 files changed

+106
-13
lines changed

8 files changed

+106
-13
lines changed

examples/firebase/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ var app = new Vue({
2929
validation: {
3030
name: false,
3131
email: false
32-
},
32+
}
33+
},
34+
computed: {
3335
isValid: {
3436
$get: function () {
3537
var valid = true

examples/firebase/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title></title>
55
<meta charset="utf-8">
66
<link rel="stylesheet" type="text/css" href="style.css">
7-
<script src='https://cdn.firebase.com/v0/firebase.js'></script>
7+
<script src='https://cdn.firebase.com/js/client/1.0.2/firebase.js'></script>
88
<script src="/js/vue.min.js"></script>
99
</head>
1010
<body>

examples/todomvc/js/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var app = new Vue({
2828

2929
data: {
3030
todos: todoStorage.fetch(),
31+
},
32+
33+
computed: {
3134
allDone: {
3235
$get: function () {
3336
return this.remaining === 0

source/api/instantiation-options.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The object must be JSON-compliant (no circular references). You can use it just
3939

4040
- **Type:** `Object`
4141

42-
Methods to be copied to the ViewModel. All methods will have their `this` context automatically bound to the ViewModel.
42+
Methods to be mixed into the ViewModel. All methods will have their `this` context automatically bound to the ViewModel instance.
4343

4444
**Example:**
4545

@@ -56,6 +56,33 @@ vm.plus()
5656
vm.a // 2
5757
```
5858

59+
### computed
60+
61+
- **Type:** `Object`
62+
63+
Computed properties to be mixed into the ViewModel. All getters and setters have their `this` context automatically bound to the ViewModel instance.
64+
65+
**Example:**
66+
67+
```js
68+
var vm = new Vue({
69+
data: { a: 1 },
70+
computed: {
71+
aplus: {
72+
$get: function () {
73+
return this.a + 1
74+
},
75+
$set: function (v) {
76+
this.a = v - 1
77+
}
78+
}
79+
}
80+
})
81+
vm.aplus // 2
82+
vm.aplus = 3
83+
vm.a // 2
84+
```
85+
5986
## DOM Element
6087

6188
### el
@@ -115,17 +142,17 @@ A hash of HTML attributes to be set on `vm.$el`.
115142

116143
All lifecycle hooks have their `this` context bound to the ViewModel they belong to.
117144

118-
### beforeCompile
145+
### created
119146

120147
- **Type:** `Function`
121-
- Alias: `created`
148+
- Alias: `beforeCompile`
122149

123150
Called before the compilation starts. Can be used to attach additional data to be observed on the ViewModel.
124151

125-
### afterCompile
152+
### ready
126153

127154
- **Type:** `Function`
128-
- Alias: `ready`
155+
- Alias: `afterCompile`
129156

130157
Called after the compilation has ended and the ViewModel is ready.
131158

source/guide/computed.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,65 @@ order: 8
55

66
# {{title}}
77

8-
Vue.js' inline expressions are very convenient, but the best use cases for them are simple boolean operations or string concatenations. For more complicated logic, you should use Computed Properties.
8+
Vue.js' inline expressions are very convenient, but the best use cases for them are simple boolean operations or string concatenations. For more complicated logic, you should use **computed properties**.
9+
10+
In Vue.js, you define computed properties with the `computed` option:
11+
12+
``` js
13+
var demo = new Vue({
14+
data: {
15+
firstName: 'Foo',
16+
lastName: 'Bar'
17+
},
18+
computed: {
19+
fullName: {
20+
// the getter should return the desired value
21+
$get: function () {
22+
return this.firstName + ' ' + this.lastName
23+
},
24+
// the setter is optional
25+
$set: function (newValue) {
26+
var names = newValue.split(' ')
27+
this.firstName = names[0]
28+
this.lastName = names[names.length - 1]
29+
}
30+
}
31+
}
32+
})
33+
demo.fullName // 'Foo Bar'
34+
```
35+
36+
A computed property is essentially a property defined with getter/setter functions. You can use a computed property just like a normal property, but when you access it, you get the value returned by the getter function; when you change its value, you trigger the setter function passing in the new value as its argument.
37+
38+
## Dependency Collection Gotcha
39+
40+
Like inline expressions, Vue.js automatically collects dependencies for computed properties and refreshes them when their dependencies change. There's one thing to keep in mind though: since Vue.js collects dependencies by monitoring which properties are accessed inside a getter, you need to be careful when there is conditional logic within your getters. Consider this example:
41+
42+
``` js
43+
status: {
44+
$get: function () {
45+
return this.validated
46+
? this.okMsg
47+
: this.errMsg
48+
}
49+
}
50+
```
51+
52+
If `validated` is true in the beginning, then `errMsg` will not be accessed and therefore not collected as a dependency. Vice-versa for `okMsg`. To get around this you can simply manually access potentially unreachable properties in your getter:
53+
54+
```
55+
status: {
56+
$get: function () {
57+
// access dependencies explicitly
58+
this.okMsg
59+
this.errMsg
60+
return this.validated
61+
? this.okMsg
62+
: this.errMsg
63+
}
64+
}
65+
```
66+
67+
You don't need to worry about this in inline expressions because Vue.js' expression parser takes care of it for you.
68+
69+
Next: [Using Transition Effects](/guide/transitions.html)

source/guide/installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ order: 1
1111

1212
Simply download and include with a script tag. `Vue` will be registered as a global variable.
1313

14-
- <a style="font-size:1.25em" href="https://raw.github.com/yyx990803/vue/v0.7.6/dist/vue.js" download>Development Version</a> <br> - 89.04kb, plenty of comments
14+
- <a style="font-size:1.25em" href="https://raw.github.com/yyx990803/vue/v0.8.0/dist/vue.js" download>Development Version</a> <br> - 87.75kb, plenty of comments
1515

16-
- <a style="font-size:1.25em" href="https://raw.github.com/yyx990803/vue/v0.7.6/dist/vue.min.js" download>Production Version</a> <br> - 31.26kb minified / 10.71kb minified + gzipped
16+
- <a style="font-size:1.25em" href="https://raw.github.com/yyx990803/vue/v0.8.0/dist/vue.min.js" download>Production Version</a> <br> - 30.81kb minified / 10.57kb minified + gzipped
1717

1818
## Component
1919

themes/vue/layout/partials/landing.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h1>Vue.js</h1>
1010
<p class="sub">Intuitive, Fast and Composable MVVM</p>
1111
<p>
12-
<a href="/guide/installation.html" class="button">Get Vue.js v0.7.6</a>
12+
<a href="/guide/installation.html" class="button">Get Vue.js v0.8.0</a>
1313
<a href="https://github.com/yyx990803/vue" target="_blank" class="button">Source on GitHub</a>
1414
</p>
1515
</div>

themes/vue/source/js/vue.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)