github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/src/directives/public/if.js (about) 1 import FragmentFactory from '../../fragment/factory' 2 import { IF } from '../priorities' 3 import { 4 getAttr, 5 remove, 6 replace, 7 createAnchor, 8 warn 9 } from '../../util/index' 10 11 export default { 12 13 priority: IF, 14 terminal: true, 15 16 bind () { 17 var el = this.el 18 if (!el.__vue__) { 19 // check else block 20 var next = el.nextElementSibling 21 if (next && getAttr(next, 'v-else') !== null) { 22 remove(next) 23 this.elseEl = next 24 } 25 // check main block 26 this.anchor = createAnchor('v-if') 27 replace(el, this.anchor) 28 } else { 29 process.env.NODE_ENV !== 'production' && warn( 30 'v-if="' + this.expression + '" cannot be ' + 31 'used on an instance root element.', 32 this.vm 33 ) 34 this.invalid = true 35 } 36 }, 37 38 update (value) { 39 if (this.invalid) return 40 if (value) { 41 if (!this.frag) { 42 this.insert() 43 } 44 } else { 45 this.remove() 46 } 47 }, 48 49 insert () { 50 if (this.elseFrag) { 51 this.elseFrag.remove() 52 this.elseFrag = null 53 } 54 // lazy init factory 55 if (!this.factory) { 56 this.factory = new FragmentFactory(this.vm, this.el) 57 } 58 this.frag = this.factory.create(this._host, this._scope, this._frag) 59 this.frag.before(this.anchor) 60 }, 61 62 remove () { 63 if (this.frag) { 64 this.frag.remove() 65 this.frag = null 66 } 67 if (this.elseEl && !this.elseFrag) { 68 if (!this.elseFactory) { 69 this.elseFactory = new FragmentFactory( 70 this.elseEl._context || this.vm, 71 this.elseEl 72 ) 73 } 74 this.elseFrag = this.elseFactory.create(this._host, this._scope, this._frag) 75 this.elseFrag.before(this.anchor) 76 } 77 }, 78 79 unbind () { 80 if (this.frag) { 81 this.frag.destroy() 82 } 83 if (this.elseFrag) { 84 this.elseFrag.destroy() 85 } 86 } 87 }