Skip to content

Commit 837b2d6

Browse files
committed
add callback to dom methods
1 parent f9bebc7 commit 837b2d6

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/viewmodel.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,37 @@ def(VMProto, '$emit', function () {
9797

9898
// DOM convenience methods
9999

100-
def(VMProto, '$appendTo', function (target) {
100+
def(VMProto, '$appendTo', function (target, cb) {
101101
target = query(target)
102102
var el = this.$el
103103
transition(el, 1, function () {
104104
target.appendChild(el)
105+
if (cb) cb()
105106
}, this.$compiler)
106107
})
107108

108-
def(VMProto, '$remove', function () {
109+
def(VMProto, '$remove', function (cb) {
109110
var el = this.$el,
110111
parent = el.parentNode
111112
if (!parent) return
112113
transition(el, -1, function () {
113114
parent.removeChild(el)
115+
if (cb) cb()
114116
}, this.$compiler)
115117
})
116118

117-
def(VMProto, '$before', function (target) {
119+
def(VMProto, '$before', function (target, cb) {
118120
target = query(target)
119121
var el = this.$el,
120122
parent = target.parentNode
121123
if (!parent) return
122124
transition(el, 1, function () {
123125
parent.insertBefore(el, target)
126+
if (cb) cb()
124127
}, this.$compiler)
125128
})
126129

127-
def(VMProto, '$after', function (target) {
130+
def(VMProto, '$after', function (target, cb) {
128131
target = query(target)
129132
var el = this.$el,
130133
parent = target.parentNode,
@@ -136,6 +139,7 @@ def(VMProto, '$after', function (target) {
136139
} else {
137140
parent.appendChild(el)
138141
}
142+
if (cb) cb()
139143
}, this.$compiler)
140144
})
141145

test/unit/specs/viewmodel.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ describe('UNIT: ViewModel', function () {
216216
describe('DOM methods', function () {
217217

218218
var enterCalled,
219-
leaveCalled
219+
leaveCalled,
220+
callbackCalled
220221

221222
var v = new Vue({
222223
attributes: {
@@ -239,25 +240,32 @@ describe('UNIT: ViewModel', function () {
239240
function reset () {
240241
enterCalled = false
241242
leaveCalled = false
243+
callbackCalled = false
244+
}
245+
246+
function cb () {
247+
callbackCalled = true
242248
}
243249

244250
it('$appendTo', function () {
245251
reset()
246252
var parent = document.createElement('div')
247-
v.$appendTo(parent)
253+
v.$appendTo(parent, cb)
248254
assert.strictEqual(v.$el.parentNode, parent)
249255
assert.ok(enterCalled)
256+
assert.ok(callbackCalled)
250257
})
251258

252259
it('$before', function () {
253260
reset()
254261
var parent = document.createElement('div'),
255262
ref = document.createElement('div')
256263
parent.appendChild(ref)
257-
v.$before(ref)
264+
v.$before(ref, cb)
258265
assert.strictEqual(v.$el.parentNode, parent)
259266
assert.strictEqual(v.$el.nextSibling, ref)
260267
assert.ok(enterCalled)
268+
assert.ok(callbackCalled)
261269
})
262270

263271
it('$after', function () {
@@ -267,27 +275,30 @@ describe('UNIT: ViewModel', function () {
267275
ref2 = document.createElement('div')
268276
parent.appendChild(ref1)
269277
parent.appendChild(ref2)
270-
v.$after(ref1)
278+
v.$after(ref1, cb)
271279
assert.strictEqual(v.$el.parentNode, parent)
272280
assert.strictEqual(v.$el.nextSibling, ref2)
273281
assert.strictEqual(ref1.nextSibling, v.$el)
274282
assert.ok(enterCalled)
283+
assert.ok(callbackCalled)
275284
reset()
276-
v.$after(ref2)
285+
v.$after(ref2, cb)
277286
assert.strictEqual(v.$el.parentNode, parent)
278287
assert.notOk(v.$el.nextSibling)
279288
assert.strictEqual(ref2.nextSibling, v.$el)
280289
assert.ok(enterCalled)
290+
assert.ok(callbackCalled)
281291
})
282292

283293
it('$remove', function () {
284294
reset()
285295
var parent = document.createElement('div')
286296
v.$appendTo(parent)
287-
v.$remove()
297+
v.$remove(cb)
288298
assert.notOk(v.$el.parentNode)
289299
assert.ok(enterCalled)
290300
assert.ok(leaveCalled)
301+
assert.ok(callbackCalled)
291302
})
292303

293304
})

0 commit comments

Comments
 (0)