Skip to content

Commit d132fdc

Browse files
committed
DOM convenience methods
1 parent 628c42c commit d132fdc

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/compiler.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var Emitter = require('./emitter'),
77
TextParser = require('./text-parser'),
88
DepsParser = require('./deps-parser'),
99
ExpParser = require('./exp-parser'),
10-
transition = require('./transition'),
1110
// cache deps ob
1211
depsOb = DepsParser.observer,
1312
// cache methods
@@ -618,10 +617,8 @@ CompilerProto.destroy = function () {
618617
// finally remove dom element
619618
if (el === document.body) {
620619
el.innerHTML = ''
621-
} else if (el.parentNode) {
622-
transition(el, -1, function () {
623-
el.parentNode.removeChild(el)
624-
}, this)
620+
} else {
621+
vm.$remove()
625622
}
626623

627624
// post teardown hook

src/viewmodel.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var Compiler = require('./compiler'),
2-
def = require('./utils').defProtected
1+
var Compiler = require('./compiler'),
2+
def = require('./utils').defProtected,
3+
transition = require('./transition')
34

45
/**
56
* ViewModel exposed to the user that holds data,
@@ -94,6 +95,56 @@ def(VMProto, '$emit', function () {
9495
})
9596
})
9697

98+
// DOM convenience methods
99+
100+
def(VMProto, '$appendTo', function (target) {
101+
target = query(target)
102+
var el = this.$el
103+
transition(el, 1, function () {
104+
target.appendChild(el)
105+
}, this.$compiler)
106+
})
107+
108+
def(VMProto, '$remove', function () {
109+
var el = this.$el,
110+
parent = el.parentNode
111+
if (!parent) return
112+
transition(el, -1, function () {
113+
parent.removeChild(el)
114+
}, this.$compiler)
115+
})
116+
117+
def(VMProto, '$before', function (target) {
118+
target = query(target)
119+
var el = this.$el,
120+
parent = target.parentNode
121+
if (!parent) return
122+
transition(el, 1, function () {
123+
parent.insertBefore(el, target)
124+
}, this.$compiler)
125+
})
126+
127+
def(VMProto, '$after', function (target) {
128+
target = query(target)
129+
var el = this.$el,
130+
parent = target.parentNode,
131+
next = target.nextSibling
132+
if (!parent) return
133+
transition(el, 1, function () {
134+
if (next) {
135+
parent.insertBefore(el, next)
136+
} else {
137+
parent.appendChild(el)
138+
}
139+
}, this.$compiler)
140+
})
141+
142+
function query (el) {
143+
return typeof el === 'string'
144+
? document.querySelector(el)
145+
: el
146+
}
147+
97148
/**
98149
* If a VM doesn't contain a path, go up the prototype chain
99150
* to locate the ancestor that has it.

test/unit/specs/viewmodel.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,9 @@ describe('UNIT: ViewModel', function () {
302302
}
303303
}
304304
},
305-
el: {
306-
getAttribute: function () {},
307-
parentNode: {
308-
removeChild: function () {
309-
elRemoved = true
310-
}
305+
vm: {
306+
$remove: function () {
307+
elRemoved = true
311308
}
312309
}
313310
}

0 commit comments

Comments
 (0)