Skip to content

Commit 55ae8dc

Browse files
committed
repeat mutation handler simplify
1 parent 7eb0843 commit 55ae8dc

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/directives/repeat.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,31 @@ var Observer = require('../observer'),
1111
var mutationHandlers = {
1212

1313
push: function (m) {
14-
var i = 0, l = m.args.length, vm,
15-
base = this.collection.length - l
16-
for (; i < l; i++) {
17-
vm = this.buildItem(m.args[i], base + i)
18-
this.updateObject(vm, 1)
19-
}
14+
this.addItems(m.args, this.vms.length)
2015
},
2116

2217
pop: function () {
2318
var vm = this.vms.pop()
24-
if (vm) {
25-
vm.$destroy()
26-
this.updateObject(vm, -1)
27-
}
19+
if (vm) this.removeItems([vm])
2820
},
2921

3022
unshift: function (m) {
31-
var i = 0, l = m.args.length, vm
32-
for (; i < l; i++) {
33-
vm = this.buildItem(m.args[i], i)
34-
this.updateObject(vm, 1)
35-
}
23+
this.addItems(m.args)
3624
},
3725

3826
shift: function () {
3927
var vm = this.vms.shift()
40-
if (vm) {
41-
vm.$destroy()
42-
this.updateObject(vm, -1)
43-
}
28+
if (vm) this.removeItems([vm])
4429
},
4530

4631
splice: function (m) {
47-
var i, l, vm,
48-
index = m.args[0],
32+
var index = m.args[0],
4933
removed = m.args[1],
50-
added = m.args.length - 2,
5134
removedVMs = removed === undefined
5235
? this.vms.splice(index)
5336
: this.vms.splice(index, removed)
54-
for (i = 0, l = removedVMs.length; i < l; i++) {
55-
removedVMs[i].$destroy()
56-
this.updateObject(removedVMs[i], -1)
57-
}
58-
for (i = 0; i < added; i++) {
59-
vm = this.buildItem(m.args[i + 2], index + i)
60-
this.updateObject(vm, 1)
61-
}
37+
this.removeItems(removedVMs)
38+
this.addItems(m.args.slice(2), index)
6239
},
6340

6441
sort: function () {
@@ -170,7 +147,7 @@ module.exports = {
170147

171148
// create new VMs and append to DOM
172149
if (collection.length) {
173-
collection.forEach(this.buildItem, this)
150+
collection.forEach(this.build, this)
174151
if (!init) this.changed()
175152
}
176153

@@ -179,6 +156,22 @@ module.exports = {
179156
this.old = this.oldVMs = null
180157
},
181158

159+
addItems: function (data, base) {
160+
base = base || 0
161+
for (var i = 0, l = data.length; i < l; i++) {
162+
var vm = this.build(data[i], base + i)
163+
this.updateObject(vm, 1)
164+
}
165+
},
166+
167+
removeItems: function (data) {
168+
var i = data.length
169+
while (i--) {
170+
data[i].$destroy()
171+
this.updateObject(data[i], -1)
172+
}
173+
},
174+
182175
/**
183176
* Notify parent compiler that new items
184177
* have been added to the collection, it needs
@@ -197,7 +190,7 @@ module.exports = {
197190
},
198191

199192
/**
200-
* Run a dry buildItem just to collect bindings
193+
* Run a dry build just to collect bindings
201194
*/
202195
dryBuild: function () {
203196
new this.Ctor({
@@ -215,7 +208,7 @@ module.exports = {
215208
* passing along compiler options indicating this
216209
* is a v-repeat item.
217210
*/
218-
buildItem: function (data, index) {
211+
build: function (data, index) {
219212

220213
var ctn = this.container,
221214
vms = this.vms,

0 commit comments

Comments
 (0)