2014-08-17 06:06:37 +08:00
|
|
|
var _ = require('../util')
|
2014-10-20 07:32:36 +08:00
|
|
|
var compile = require('../compile/compile')
|
2014-08-18 03:36:13 +08:00
|
|
|
var templateParser = require('../parse/template')
|
2014-10-20 07:32:36 +08:00
|
|
|
var transition = require('../transition')
|
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-10-20 07:32:36 +08:00
|
|
|
this.isBlock = el.tagName === 'TEMPLATE'
|
|
|
|
|
this.template = this.isBlock
|
|
|
|
|
? templateParser.parse(el, true)
|
|
|
|
|
: el
|
|
|
|
|
// compile the nested partial
|
|
|
|
|
this.linker = compile(
|
|
|
|
|
this.template,
|
|
|
|
|
this.vm.$options,
|
|
|
|
|
true
|
|
|
|
|
)
|
2014-08-18 03:36:13 +08:00
|
|
|
} else {
|
2014-08-28 06:59:38 +08:00
|
|
|
this.invalid = true
|
2014-08-18 03:36:13 +08:00
|
|
|
_.warn(
|
2014-08-28 06:59:38 +08:00
|
|
|
'v-if="' + this.expression + '" cannot be ' +
|
2014-08-18 03:36:13 +08:00
|
|
|
'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) {
|
2014-08-28 06:59:38 +08:00
|
|
|
if (this.invalid) return
|
2014-08-18 03:36:13 +08:00
|
|
|
if (value) {
|
2014-10-20 07:32:36 +08:00
|
|
|
this.insert()
|
|
|
|
|
} else {
|
2014-10-21 09:17:39 +08:00
|
|
|
this.teardown()
|
2014-10-20 07:32:36 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
insert: function () {
|
|
|
|
|
var vm = this.vm
|
|
|
|
|
var el = templateParser.clone(this.template)
|
|
|
|
|
var ref = this.ref
|
|
|
|
|
var decompile = this.linker(vm, el)
|
|
|
|
|
if (this.isBlock) {
|
|
|
|
|
var blockStart = el.firstChild
|
|
|
|
|
this.decompile = function () {
|
|
|
|
|
decompile()
|
|
|
|
|
var node = blockStart
|
|
|
|
|
var next
|
|
|
|
|
while (node !== ref) {
|
|
|
|
|
next = node.nextSibling
|
|
|
|
|
transition.remove(node, vm)
|
|
|
|
|
node = next
|
2014-08-18 03:36:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-10-20 07:32:36 +08:00
|
|
|
this.decompile = function () {
|
|
|
|
|
decompile()
|
|
|
|
|
transition.remove(el, vm)
|
2014-08-18 03:36:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
2014-10-20 07:32:36 +08:00
|
|
|
transition.before(el, ref, vm)
|
2014-08-18 03:36:13 +08:00
|
|
|
},
|
|
|
|
|
|
2014-10-21 09:17:39 +08:00
|
|
|
teardown: function () {
|
2014-10-20 07:32:36 +08:00
|
|
|
if (this.decompile) {
|
|
|
|
|
this.decompile()
|
|
|
|
|
this.decompile = null
|
2014-08-18 03:36:13 +08:00
|
|
|
}
|
2014-08-17 06:06:37 +08:00
|
|
|
}
|
2014-08-18 02:13:52 +08:00
|
|
|
|
2014-08-17 06:06:37 +08:00
|
|
|
}
|