Skip to content

Commit 5d934bf

Browse files
committed
reduce code duplication on DOM api
1 parent 957104e commit 5d934bf

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/api/dom.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ var transition = require('../transition')
1010
*/
1111

1212
exports.$appendTo = function (target, cb, withTransition) {
13-
target = query(target)
14-
var targetIsDetached = !_.inDoc(target)
15-
var op = withTransition === false || targetIsDetached
16-
? append
17-
: transition.append
18-
insert(this, target, op, targetIsDetached, cb)
19-
return this
13+
return insert(
14+
this, target, cb, withTransition,
15+
append, transition.append
16+
)
2017
}
2118

2219
/**
@@ -46,13 +43,10 @@ exports.$prependTo = function (target, cb, withTransition) {
4643
*/
4744

4845
exports.$before = function (target, cb, withTransition) {
49-
target = query(target)
50-
var targetIsDetached = !_.inDoc(target)
51-
var op = withTransition === false || targetIsDetached
52-
? before
53-
: transition.before
54-
insert(this, target, op, targetIsDetached, cb)
55-
return this
46+
return insert(
47+
this, target, cb, withTransition,
48+
before, transition.before
49+
)
5650
}
5751

5852
/**
@@ -113,12 +107,19 @@ exports.$remove = function (cb, withTransition) {
113107
*
114108
* @param {Vue} vm
115109
* @param {Element} target
116-
* @param {Function} op
117-
* @param {Boolean} targetIsDetached
118110
* @param {Function} [cb]
111+
* @param {Boolean} [withTransition]
112+
* @param {Function} op1 - op for non-transition insert
113+
* @param {Function} op2 - op for transition insert
114+
* @return vm
119115
*/
120116

121-
function insert (vm, target, op, targetIsDetached, cb) {
117+
function insert (vm, target, cb, withTransition, op1, op2) {
118+
target = query(target)
119+
var targetIsDetached = !_.inDoc(target)
120+
var op = withTransition === false || targetIsDetached
121+
? op1
122+
: op2
122123
var shouldCallHook =
123124
!targetIsDetached &&
124125
!vm._isAttached &&
@@ -131,6 +132,7 @@ function insert (vm, target, op, targetIsDetached, cb) {
131132
if (shouldCallHook) {
132133
vm._callHook('attached')
133134
}
135+
return vm
134136
}
135137

136138
/**

0 commit comments

Comments
 (0)