Skip to content

Commit 48e328c

Browse files
committed
output Array in text bindings
1 parent 9af11d0 commit 48e328c

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

src/compiler.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,10 @@ CompilerProto.setupObserver = function () {
184184

185185
// add own listeners which trigger binding updates
186186
observer
187-
.on('get', function (key) {
188-
check(key)
189-
DepsParser.catcher.emit('get', bindings[key])
190-
})
191-
.on('set', function (key, val) {
192-
observer.emit('change:' + key, val)
193-
check(key)
194-
bindings[key].update(val)
195-
})
196-
.on('mutate', function (key, val, mutation) {
197-
observer.emit('change:' + key, val, mutation)
198-
check(key)
199-
bindings[key].pub()
200-
})
201-
187+
.on('get', onGet)
188+
.on('set', onSet)
189+
.on('mutate', onSet)
190+
202191
// register hooks
203192
hooks.forEach(function (hook) {
204193
var fns = options[hook]
@@ -214,6 +203,17 @@ CompilerProto.setupObserver = function () {
214203
}
215204
})
216205

206+
function onGet (key) {
207+
check(key)
208+
DepsParser.catcher.emit('get', bindings[key])
209+
}
210+
211+
function onSet (key, val, mutation) {
212+
observer.emit('change:' + key, val, mutation)
213+
check(key)
214+
bindings[key].update(val)
215+
}
216+
217217
function register (hook, fn) {
218218
observer.on('hook:' + hook, function () {
219219
fn.call(compiler.vm, options)
@@ -258,11 +258,15 @@ CompilerProto.observeData = function (data) {
258258
})
259259

260260
// emit $data change on all changes
261-
observer.on('set', function (key) {
261+
observer
262+
.on('set', onSet)
263+
.on('mutate', onSet)
264+
265+
function onSet (key) {
262266
if (key !== '$data') {
263267
$dataBinding.update(compiler.data)
264268
}
265-
})
269+
}
266270
}
267271

268272
/**

src/directive.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ function parseFilter (filter, compiler) {
120120
* during initialization.
121121
*/
122122
DirProto.update = function (value, init) {
123-
if (!init && value === this.value && utils.typeOf(value) !== 'Object') return
124-
this.value = value
125-
if (this._update) {
126-
this._update(
127-
this.filters
128-
? this.applyFilters(value)
129-
: value
130-
)
123+
var type = utils.typeOf(value)
124+
if (init || value !== this.value || type === 'Object' || type === 'Array') {
125+
this.value = value
126+
if (this._update) {
127+
this._update(
128+
this.filters
129+
? this.applyFilters(value)
130+
: value
131+
)
132+
}
131133
}
132134
}
133135

src/directives/repeat.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ module.exports = {
119119
},
120120

121121
update: function (collection, init) {
122+
123+
if (collection === this.collection) return
122124

123125
this.reset()
124126
// attach an object to container to hold handlers

test/functional/fixtures/output-object.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div id="test">
22
<p id="data">{{$data}}</p>
33
<p id="obj">{{test}}</p>
4+
<p id="arr">{{arr}}</p>
45
</div>
56

67
<script src="../../../dist/vue.js"></script>
@@ -11,7 +12,8 @@
1112
data: {
1213
test: {
1314
prop: 1
14-
}
15-
}
15+
},
16+
arr: [{a: 1}]
17+
}
1618
})
1719
</script>

test/functional/specs/output-object.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
1-
casper.test.begin('Outputting Objects', 8, function (test) {
1+
casper.test.begin('Outputting Objects', 15, function (test) {
22

33
casper
44
.start('./fixtures/output-object.html')
55
.then(function () {
6-
test.assertSelectorHasText('#data', '{"test":{"prop":1}}')
6+
test.assertSelectorHasText('#data', '{"test":{"prop":1},"arr":[{"a":1}]}')
77
test.assertSelectorHasText('#obj', '{"prop":1}')
8+
test.assertSelectorHasText('#arr', '[{"a":1}]')
89
})
910
// setting a nested property
1011
.thenEvaluate(function () {
1112
test.test.prop = 2
1213
})
1314
.then(function () {
14-
test.assertSelectorHasText('#data', '{"test":{"prop":2}}')
15+
test.assertSelectorHasText('#data', '{"test":{"prop":2},"arr":[{"a":1}]}')
1516
test.assertSelectorHasText('#obj', '{"prop":2}')
1617
})
1718
// setting a nested object
1819
.thenEvaluate(function () {
1920
test.test = { hi:3 }
2021
})
2122
.then(function () {
22-
test.assertSelectorHasText('#data', '{"test":{"hi":3}}')
23+
test.assertSelectorHasText('#data', '{"test":{"hi":3},"arr":[{"a":1}]}')
2324
test.assertSelectorHasText('#obj', '{"hi":3}')
2425
})
26+
// mutating an array
27+
.thenEvaluate(function () {
28+
test.arr.push({a:2})
29+
})
30+
.then(function () {
31+
test.assertSelectorHasText('#data', '{"test":{"hi":3},"arr":[{"a":1},{"a":2}]}')
32+
test.assertSelectorHasText('#arr', '[{"a":1},{"a":2}]')
33+
})
34+
// no length change mutate an array
35+
.thenEvaluate(function () {
36+
test.arr.reverse()
37+
})
38+
.then(function () {
39+
test.assertSelectorHasText('#data', '{"test":{"hi":3},"arr":[{"a":2},{"a":1}]}')
40+
test.assertSelectorHasText('#arr', '[{"a":2},{"a":1}]')
41+
})
42+
// swap the array
43+
.thenEvaluate(function () {
44+
test.arr = [1,2,3]
45+
})
46+
.then(function () {
47+
test.assertSelectorHasText('#data', '{"test":{"hi":3},"arr":[1,2,3]}')
48+
test.assertSelectorHasText('#arr', '[1,2,3]')
49+
})
2550
// setting $data
2651
.thenEvaluate(function () {
27-
test.$data = { test: { swapped: true } }
52+
test.$data = { test: { swapped: true }, arr:[3,2,1] }
2853
})
2954
.then(function () {
30-
test.assertSelectorHasText('#data', '{"test":{"swapped":true}}')
55+
test.assertSelectorHasText('#data', '{"test":{"swapped":true},"arr":[3,2,1]}')
3156
test.assertSelectorHasText('#obj', '{"swapped":true}')
3257
})
3358
.run(function () {

0 commit comments

Comments
 (0)