Skip to content

Commit

Permalink
alias for v-repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan You committed Mar 7, 2014
1 parent 835dd0e commit cea54de
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/directives/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ module.exports = {
// so that it can still work in a detached state
el.vue_if_parent = ctn
el.vue_if_ref = ref

// we have an alias, wrap the data
if (self.arg) {
var actual = data
data = {}
data[self.arg] = actual
}

// wrap non-object value in an object
nonObject = utils.typeOf(data) !== 'Object'
if (nonObject) {
Expand All @@ -268,14 +276,15 @@ module.exports = {
repeat: true
}
})
// for non-object values, listen for value change
// for non-object values or aliased items, listen for value change
// so we can sync it back to the original Array
if (nonObject) {
item.$compiler.observer.on('change:$value', function (val) {
if (nonObject || self.arg) {
var sync = function (val) {
self.lock = true
self.collection.set(item.$index, val)
self.lock = false
})
}
item.$compiler.observer.on('change:' + (self.arg || '$value'), sync)
}

}
Expand Down
38 changes: 38 additions & 0 deletions test/unit/specs/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,44 @@ describe('Directives', function () {

})

it('should accept arg for aliasing on primitive arrays', function (done) {

var v = new Vue({
template: '<span v-repeat="item:items" v-ref="items">{{item}}</span>',
data: {
items: [1,2,3]
}
})
assert.strictEqual(v.$el.textContent, '123')
v.$.items[0].item = 2

nextTick(function () {
assert.strictEqual(v.$el.textContent, '223')
assert.deepEqual(v.items, [2,2,3])
done()
})

})

it('should accept arg for aliasing on object arrays', function (done) {

var v = new Vue({
template: '<span v-repeat="item:items" v-ref="items">{{item.id}}</span>',
data: {
items: [{id:1},{id:2},{id:3}]
}
})
assert.strictEqual(v.$el.textContent, '123')
v.$.items[0].item = { id: 2 }

nextTick(function () {
assert.strictEqual(v.$el.textContent, '223')
assert.strictEqual(v.items[0].id, 2)
done()
})

})

})

describe('style', function () {
Expand Down

0 comments on commit cea54de

Please sign in to comment.