Skip to content

Commit 705c8b0

Browse files
committed
update doc for computed shorthand, minor fix for options
1 parent 9784a47 commit 705c8b0

File tree

4 files changed

+79
-41
lines changed

4 files changed

+79
-41
lines changed

perf/todomvc-benchmark/vue/bower_components/vue/dist/vue.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
VueJS v0.8.0
2+
VueJS v0.8.1
33
(c) 2014 Evan You
44
License: MIT
55
*/
@@ -843,6 +843,7 @@ function Compiler (vm, options) {
843843
compiler.vm = vm
844844
compiler.bindings = makeHash()
845845
compiler.dirs = []
846+
compiler.deferred = []
846847
compiler.exps = []
847848
compiler.computed = []
848849
compiler.childCompilers = []
@@ -915,11 +916,14 @@ function Compiler (vm, options) {
915916
// and bind the parsed directives
916917
compiler.compile(el, true)
917918

918-
// extract dependencies for computed properties
919-
if (compiler.computed.length) {
920-
DepsParser.parse(compiler.computed)
919+
// bind deferred directives (child components)
920+
for (var i = 0, l = compiler.deferred.length; i < l; i++) {
921+
compiler.bindDirective(compiler.deferred[i])
921922
}
922923

924+
// extract dependencies for computed properties
925+
compiler.parseDeps()
926+
923927
// done!
924928
compiler.init = false
925929

@@ -1045,7 +1049,10 @@ CompilerProto.compile = function (node, root) {
10451049
directive = Directive.parse('repeat', repeatExp, compiler, node)
10461050
if (directive) {
10471051
directive.Ctor = componentCtor
1048-
compiler.bindDirective(directive)
1052+
// defer child component compilation
1053+
// so by the time they are compiled, the parent
1054+
// would have collected all bindings
1055+
compiler.deferred.push(directive)
10491056
}
10501057

10511058
// v-with has 2nd highest priority
@@ -1054,7 +1061,7 @@ CompilerProto.compile = function (node, root) {
10541061
directive = Directive.parse('with', withKey || '', compiler, node)
10551062
if (directive) {
10561063
directive.Ctor = componentCtor
1057-
compiler.bindDirective(directive)
1064+
compiler.deferred.push(directive)
10581065
}
10591066

10601067
} else {
@@ -1315,10 +1322,7 @@ CompilerProto.defineProp = function (key, binding) {
13151322
CompilerProto.defineExp = function (key, binding) {
13161323
var getter = ExpParser.parse(key, this)
13171324
if (getter) {
1318-
var value = binding.isFn
1319-
? getter
1320-
: { $get: getter }
1321-
this.markComputed(binding, value)
1325+
this.markComputed(binding, getter)
13221326
this.exps.push(binding)
13231327
}
13241328
}
@@ -1329,10 +1333,8 @@ CompilerProto.defineExp = function (key, binding) {
13291333
CompilerProto.defineComputed = function (key, binding, value) {
13301334
this.markComputed(binding, value)
13311335
var def = {
1332-
get: binding.value.$get
1333-
}
1334-
if (binding.value.$set) {
1335-
def.set = binding.value.$set
1336+
get: binding.value.$get,
1337+
set: binding.value.$set
13361338
}
13371339
Object.defineProperty(this.vm, key, def)
13381340
}
@@ -1342,15 +1344,19 @@ CompilerProto.defineComputed = function (key, binding, value) {
13421344
* so its getter/setter are bound to proper context
13431345
*/
13441346
CompilerProto.markComputed = function (binding, value) {
1345-
binding.value = value
13461347
binding.isComputed = true
13471348
// bind the accessors to the vm
1348-
if (!binding.isFn) {
1349-
binding.value = {
1350-
$get: utils.bind(value.$get, this.vm)
1349+
if (binding.isFn) {
1350+
binding.value = value
1351+
} else {
1352+
if (typeof value === 'function') {
1353+
value = { $get: value }
13511354
}
1352-
if (value.$set) {
1353-
binding.value.$set = utils.bind(value.$set, this.vm)
1355+
binding.value = {
1356+
$get: utils.bind(value.$get, this.vm),
1357+
$set: value.$set
1358+
? utils.bind(value.$set, this.vm)
1359+
: undefined
13541360
}
13551361
}
13561362
// keep track for dep parsing later
@@ -1390,6 +1396,15 @@ CompilerProto.hasKey = function (key) {
13901396
hasOwn.call(this.vm, baseKey)
13911397
}
13921398

1399+
/**
1400+
* Collect dependencies for computed properties
1401+
*/
1402+
CompilerProto.parseDeps = function () {
1403+
if (this.computed.length) {
1404+
DepsParser.parse(this.computed)
1405+
}
1406+
}
1407+
13931408
/**
13941409
* Unbind and remove element
13951410
*/
@@ -2477,6 +2492,7 @@ function catchDeps (binding) {
24772492
if (binding.isFn) return
24782493
utils.log('\n- ' + binding.key)
24792494
var got = utils.hash()
2495+
binding.deps = []
24802496
catcher.on('get', function (dep) {
24812497
var has = got[dep.key]
24822498
if (has && has.compiler === dep.compiler) return
@@ -2502,7 +2518,8 @@ module.exports = {
25022518
parse: function (bindings) {
25032519
utils.log('\nparsing dependencies...')
25042520
Observer.shouldGet = true
2505-
bindings.forEach(catchDeps)
2521+
var i = bindings.length
2522+
while (i--) { catchDeps(bindings[i]) }
25062523
Observer.shouldGet = false
25072524
utils.log('\ndone.')
25082525
}
@@ -3010,11 +3027,15 @@ module.exports = {
30103027
if (method !== 'push' && method !== 'pop') {
30113028
self.updateIndexes()
30123029
}
3030+
if (method !== 'sort' && method !== 'reverse') {
3031+
// re-calculate dependencies
3032+
self.compiler.parseDeps()
3033+
}
30133034
}
30143035

30153036
},
30163037

3017-
update: function (collection) {
3038+
update: function (collection, init) {
30183039

30193040
this.unbind(true)
30203041
// attach an object to container to hold handlers
@@ -3039,6 +3060,10 @@ module.exports = {
30393060
for (var i = 0, l = collection.length; i < l; i++) {
30403061
this.buildItem(collection[i], i)
30413062
}
3063+
if (!init) {
3064+
// re-calculate dependencies
3065+
this.compiler.parseDeps()
3066+
}
30423067
}
30433068
},
30443069

source/api/instantiation-options.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ All lifecycle hooks have their `this` context bound to the ViewModel they belong
145145
### created
146146

147147
- **Type:** `Function`
148-
- Alias: `beforeCompile`
148+
- **Alias:** `beforeCompile`
149149

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

152152
### ready
153153

154154
- **Type:** `Function`
155-
- Alias: `afterCompile`
155+
- **Alias:** `afterCompile`
156156

157157
Called after the compilation has ended and the ViewModel is ready.
158158

@@ -219,5 +219,6 @@ A hash of transitions to be made available to the ViewModel. For details see [Us
219219
### lazy
220220

221221
- **Type:** `Boolean`
222+
- **Default:** `false`
222223

223224
Whether to trigger `v-model` updates only on `change` event (hit enter or lose focus) or on every `input` event (on every keystroke).

source/guide/computed.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,47 @@ var demo = new Vue({
3333
demo.fullName // 'Foo Bar'
3434
```
3535

36+
When you only need the getter, you can provide a single function instead of an object:
37+
38+
``` js
39+
// ...
40+
computed: {
41+
fullName: function () {
42+
return this.firstName + ' ' + this.lastName
43+
}
44+
}
45+
// ...
46+
```
47+
3648
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.
3749

3850
## Dependency Collection Gotcha
3951

4052
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:
4153

4254
``` js
43-
status: {
44-
$get: function () {
45-
return this.validated
46-
? this.okMsg
47-
: this.errMsg
48-
}
55+
// ...
56+
status: function () {
57+
return this.validated
58+
? this.okMsg
59+
: this.errMsg
4960
}
61+
// ...
5062
```
5163

5264
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:
5365

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-
}
66+
``` js
67+
// ...
68+
status: function () {
69+
// access dependencies explicitly
70+
this.okMsg
71+
this.errMsg
72+
return this.validated
73+
? this.okMsg
74+
: this.errMsg
6475
}
76+
// ...
6577
```
6678

6779
<p class="tip">You don't need to worry about this in inline expressions because Vue.js' expression parser takes care of it for you.</p>

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)