vue2/src/directives/if.js

54 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-08-17 06:06:37 +08:00
var _ = require('../util')
2014-08-18 03:36:13 +08:00
var templateParser = require('../parse/template')
2014-08-17 06:06:37 +08:00
module.exports = {
bind: function () {
2014-08-18 03:36:13 +08:00
var el = this.el
if (!el.__vue__) {
this.ref = document.createComment('v-if')
2014-08-21 11:00:27 +08:00
_.replace(el, this.ref)
2014-08-18 03:36:13 +08:00
this.inserted = false
if (el.tagName === 'TEMPLATE') {
this.el = templateParser.parse(el)
}
} else {
_.warn(
'v-if ' + this.expression + ' cannot be ' +
'used on an already mounted instance.'
)
}
2014-08-17 06:06:37 +08:00
},
2014-08-18 03:36:13 +08:00
update: function (value) {
if (value) {
if (!this.inserted) {
if (!this.childVM) {
this.build()
}
this.childVM.$before(this.ref)
this.inserted = true
}
} else {
if (this.inserted) {
this.childVM.$remove()
this.inserted = false
}
}
},
build: function () {
2014-08-25 11:47:36 +08:00
this.childVM = this.vm._addChild({
2014-08-18 03:36:13 +08:00
el: this.el,
parent: this.vm,
2014-08-25 12:25:02 +08:00
anonymous: true
2014-08-18 03:36:13 +08:00
})
2014-08-17 06:06:37 +08:00
},
unbind: function () {
2014-08-18 03:36:13 +08:00
if (this.childVM) {
this.childVM.$destroy()
}
2014-08-17 06:06:37 +08:00
}
2014-08-18 02:13:52 +08:00
2014-08-17 06:06:37 +08:00
}