Skip to content

Commit 2ca2d77

Browse files
committed
make v-component-id return an Array when used with v-repeat
1 parent fb20e6d commit 2ca2d77

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/directives/repeat.js

+32-25
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,28 @@ module.exports = {
8585

8686
bind: function () {
8787

88-
var self = this,
89-
el = self.el,
90-
ctn = self.container = el.parentNode
88+
var el = this.el,
89+
ctn = this.container = el.parentNode
9190

9291
// extract child VM information, if any
9392
ViewModel = ViewModel || require('../viewmodel')
94-
self.Ctor = self.Ctor || ViewModel
95-
93+
this.Ctor = this.Ctor || ViewModel
9694
// extract transition information
97-
self.hasTrans = el.hasAttribute(config.attrs.transition)
95+
this.hasTrans = el.hasAttribute(config.attrs.transition)
96+
// extract child Id, if any
97+
this.childId = utils.attr(el, 'component-id')
9898

9999
// create a comment node as a reference node for DOM insertions
100-
self.ref = document.createComment(config.prefix + '-repeat-' + self.key)
101-
ctn.insertBefore(self.ref, el)
100+
this.ref = document.createComment(config.prefix + '-repeat-' + this.key)
101+
ctn.insertBefore(this.ref, el)
102102
ctn.removeChild(el)
103103

104-
self.initiated = false
105-
self.collection = null
106-
self.vms = null
107-
self.mutationListener = function (path, arr, mutation) {
104+
this.initiated = false
105+
this.collection = null
106+
this.vms = null
107+
108+
var self = this
109+
this.mutationListener = function (path, arr, mutation) {
108110
var method = mutation.method
109111
mutationHandlers[method].call(self, mutation)
110112
if (method !== 'push' && method !== 'pop') {
@@ -119,31 +121,33 @@ module.exports = {
119121

120122
update: function (collection, init) {
121123

122-
var self = this
123-
self.unbind(true)
124+
this.unbind(true)
124125
// attach an object to container to hold handlers
125-
self.container.vue_dHandlers = utils.hash()
126+
this.container.vue_dHandlers = utils.hash()
126127
// if initiating with an empty collection, we need to
127128
// force a compile so that we get all the bindings for
128129
// dependency extraction.
129-
if (!self.initiated && (!collection || !collection.length)) {
130-
self.buildItem()
131-
self.initiated = true
130+
if (!this.initiated && (!collection || !collection.length)) {
131+
this.buildItem()
132+
this.initiated = true
133+
}
134+
collection = this.collection = collection || []
135+
this.vms = []
136+
if (this.childId) {
137+
this.vm.$[this.childId] = this.vms
132138
}
133-
collection = self.collection = collection || []
134-
self.vms = []
135139

136140
// listen for collection mutation events
137141
// the collection has been augmented during Binding.set()
138142
if (!collection.__observer__) Observer.watchArray(collection, null, new Emitter())
139-
collection.__observer__.on('mutate', self.mutationListener)
143+
collection.__observer__.on('mutate', this.mutationListener)
140144

141145
// create child-vms and append to DOM
142146
if (collection.length) {
143147
for (var i = 0, l = collection.length; i < l; i++) {
144-
self.buildItem(collection[i], i)
148+
this.buildItem(collection[i], i)
145149
}
146-
if (!init) self.changed()
150+
if (!init) this.changed()
147151
}
148152
},
149153

@@ -154,9 +158,9 @@ module.exports = {
154158
* Batched to ensure it's called only once every event loop.
155159
*/
156160
changed: function () {
161+
if (this.queued) return
162+
this.queued = true
157163
var self = this
158-
if (self.queued) return
159-
self.queued = true
160164
setTimeout(function () {
161165
self.compiler.parseDeps()
162166
self.queued = false
@@ -221,6 +225,9 @@ module.exports = {
221225
},
222226

223227
unbind: function () {
228+
if (this.childId) {
229+
delete this.vm.$[this.childId]
230+
}
224231
if (this.collection) {
225232
this.collection.__observer__.off('mutate', this.mutationListener)
226233
var i = this.vms.length

test/functional/fixtures/repeated-vms.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script src="../../../dist/vue.js"></script>
77
</head>
88
<body>
9-
<div class="item" v-repeat="items" v-component="item" v-on="click:click">
9+
<div class="item" v-repeat="items" v-component="item" v-component-id="items" v-on="click:click">
1010
{{msg + ' ' + title}}
1111
</div>
1212
<script>
@@ -23,10 +23,15 @@
2323
},
2424
data: {
2525
msg: 'msg'
26+
},
27+
computed: {
28+
reversed: function () {
29+
return this.title.split('').reverse().join('')
30+
}
2631
}
2732
})
2833

29-
new Vue({
34+
var app = new Vue({
3035
el: 'body',
3136
data: {
3237
items: [

test/functional/specs/repeated-vms.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
casper.test.begin('Repeated ViewModels', 7, function (test) {
1+
/* global app */
2+
casper.test.begin('Repeated ViewModels', 8, function (test) {
23

34
casper
45
.start('./fixtures/repeated-vms.html')
@@ -23,6 +24,15 @@ casper.test.begin('Repeated ViewModels', 7, function (test) {
2324
.thenClick('.item:nth-child(1)', function () {
2425
test.assertSelectorHasText('.item:nth-child(1)', 'msg a init click click')
2526
})
27+
.then(function () {
28+
test.assertEvalEquals(
29+
function () {
30+
return app.$.items[0].reversed
31+
},
32+
'a init click click'.split('').reverse().join(''),
33+
'should be able to access repeated vms with v-component-id'
34+
)
35+
})
2636
.run(function () {
2737
test.done()
2838
})

0 commit comments

Comments
 (0)