Skip to content

Commit 7a6169c

Browse files
committed
separate computed properties into computed option
1 parent 62fd49a commit 7a6169c

File tree

8 files changed

+108
-35
lines changed

8 files changed

+108
-35
lines changed

examples/firebase/app.js

Lines changed: 6 additions & 3 deletions
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
@@ -45,13 +47,14 @@ var app = new Vue({
4547
methods: {
4648
addUser: function (e) {
4749
e.preventDefault()
50+
console.log(this)
4851
if (this.isValid) {
4952
Users.push(this.newUser)
5053
this.newUser = {}
5154
}
5255
},
53-
removeUser: function (e) {
54-
new Firebase(baseURL + 'users/' + e.targetVM.id).remove()
56+
removeUser: function (user) {
57+
new Firebase(baseURL + 'users/' + user.id).remove()
5558
}
5659
}
5760
})

examples/firebase/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ul>
1313
<li class="user" v-repeat="users" v-transition>
1414
<span>{{name}} - {{email}}</span>
15-
<button v-on="click:removeUser">X</button>
15+
<button v-on="click:removeUser(this)">X</button>
1616
</li>
1717
</ul>
1818
<form id="form" v-on="submit:addUser">

examples/todomvc/js/app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ var app = new Vue({
3737

3838
// data
3939
data: {
40-
// fetch the saved todos from localStorage
4140
todos: todoStorage.fetch(),
42-
// a computed property with custom getter/setter
41+
},
42+
43+
// computed property
44+
computed: {
4345
allDone: {
4446
$get: function () {
4547
return this.remaining === 0

src/compiler.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ function Compiler (vm, options) {
7272
// setup observer
7373
compiler.setupObserver()
7474

75+
// create bindings for computed properties
76+
var computed = options.computed
77+
if (computed) {
78+
for (var key in computed) {
79+
compiler.createBinding(key)
80+
}
81+
}
82+
7583
// beforeCompile hook
7684
compiler.execHook('beforeCompile', 'created')
7785

@@ -482,21 +490,24 @@ CompilerProto.define = function (key, binding) {
482490
var compiler = this,
483491
data = compiler.data,
484492
vm = compiler.vm,
485-
ob = data.__observer__
486-
487-
if (!(key in data)) {
488-
data[key] = undefined
489-
}
493+
comps = compiler.options.computed,
494+
ob = data.__observer__,
495+
value
490496

491-
// if the data object is already observed, but the key
492-
// is not observed, we need to add it to the observed keys.
493-
if (ob && !(key in ob.values)) {
494-
Observer.convert(data, key)
495-
}
496-
497-
var value = binding.value = data[key]
498-
if (utils.typeOf(value) === 'Object' && value.$get) {
497+
if (comps && comps[key]) {
498+
// computed property
499+
value = binding.value = comps[key]
499500
compiler.markComputed(binding)
501+
} else {
502+
if (!(key in data)) {
503+
data[key] = undefined
504+
}
505+
// if the data object is already observed, but the key
506+
// is not observed, we need to add it to the observed keys.
507+
if (ob && !(key in ob.values)) {
508+
Observer.convert(data, key)
509+
}
510+
value = binding.value = data[key]
500511
}
501512

502513
Object.defineProperty(vm, key, {

test/functional/fixtures/nested-props.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ <h3>Computed property that concats the two: <span v-text="d"></span></h3>
2929
this.a = data
3030
},
3131
data: {
32+
hidden: {
33+
a: 1,
34+
b: 2
35+
}
36+
},
37+
computed: {
3238
d: {
3339
$get: function () {
3440
return this.msg + (this.a.b.c || '') + (this.a.c || '')
3541
}
3642
},
37-
hidden: {
38-
a: 1,
39-
b: 2
40-
},
4143
sum: {
4244
$get: function () {
4345
return this.hidden.a + this.hidden.b

test/functional/fixtures/share-data.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
new Vue({
4242
el: '#d',
4343
data: {
44-
shared: shared,
44+
shared: shared
45+
},
46+
computed: {
4547
source: {
4648
$get: function () {
4749
return JSON.stringify(this.shared)

test/functional/fixtures/validation.html

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,59 @@
1212
</head>
1313
<body>
1414
<div id="test">
15-
email: <input type="text" v-model="a | email" v-class="valid:validation.email" name="email">
15+
name: <input type="text" v-model="name | nameValidator" v-class="valid:validation.name" name="name">
16+
email: <input type="text" v-model="email | emailValidator" v-class="valid:validation.email" name="email">
17+
<a id="go" v-on="click:go">Go</a>
18+
<ul>
19+
<li class="user" v-repeat="users">
20+
{{name}} {{email}}
21+
</li>
22+
</ul>
1623
</div>
1724
<script>
1825
var RE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
1926
var test = new Vue({
2027
el: '#test',
2128
filters: {
22-
email: function (val) {
23-
this.validation.email = val === '' || RE.test(val)
29+
nameValidator: function (val) {
30+
this.validation.name = !!val
31+
return val
32+
},
33+
emailValidator: function (val) {
34+
this.validation.email = RE.test(val)
2435
return val
2536
}
2637
},
2738
data: {
28-
a: 'hihi',
39+
name: '',
40+
email: '',
41+
users: [],
2942
validation: {
30-
email: true
43+
email: false,
44+
name: false
45+
}
46+
},
47+
computed: {
48+
isValid: {
49+
$get: function () {
50+
var valid = true
51+
for (var key in this.validation) {
52+
if (!this.validation[key]) {
53+
valid = false
54+
}
55+
}
56+
return valid
57+
}
58+
}
59+
},
60+
methods: {
61+
go: function () {
62+
if (this.isValid) {
63+
this.users.push({
64+
name: this.name,
65+
email: this.email
66+
})
67+
}
3168
}
3269
}
3370
})

test/functional/specs/validation.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
casper.test.begin('Validation', 4, function (test) {
1+
casper.test.begin('Validation', 9, function (test) {
22

33
casper
44
.start('./fixtures/validation.html')
55
.then(function () {
66
test.assertElementCount('.valid', 0)
7-
this.sendKeys('input', '@hello.com')
7+
this.sendKeys('input[name="name"]', 'haha')
88
})
99
.then(function () {
1010
test.assertElementCount('.valid', 1)
11+
this.sendKeys('input[name="email"]', 'hello')
1112
})
13+
.then(function () {
14+
// email should be invalid
15+
test.assertElementCount('.valid', 1)
16+
this.sendKeys('input[name="email"]', '[email protected]', { reset: true })
17+
})
18+
.then(function () {
19+
// email should be valid now
20+
test.assertField('email', '[email protected]')
21+
test.assertElementCount('.valid', 2)
22+
})
23+
// test edit/insertion when there are filters
1224
.thenEvaluate(function () {
13-
document.querySelector('input').setSelectionRange(4,4)
25+
document.querySelector('input[name="email"]').setSelectionRange(2,2)
1426
})
1527
.then(function () {
16-
this.sendKeys('input', 'hoho')
28+
this.sendKeys('input[name="email"]', 'll')
1729
})
1830
.then(function () {
19-
test.assertElementCount('.valid', 1)
20-
test.assertField('email', '[email protected]')
31+
test.assertElementCount('.valid', 2)
32+
test.assertField('email', '[email protected]')
33+
})
34+
.thenClick('#go', function () {
35+
test.assertElementCount('.user', 1)
36+
test.assertSelectorHasText('.user', 'haha [email protected]')
2137
})
2238
.run(function () {
2339
test.done()

0 commit comments

Comments
 (0)