Skip to content

Commit

Permalink
add inject directive
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyzx committed Jan 8, 2016
1 parent 224151f commit 8430843
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const transitionRE = /^(v-bind:|:)?transition$/
// terminal directives
const terminalDirectives = [
'for',
'if'
'if',
'inject'
]

// default directive priority
Expand Down
1 change: 1 addition & 0 deletions src/directives/priorities.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export const PARTIAL = 1750
export const SLOT = 1750
export const FOR = 2000
export const IF = 2000
export const INJECT = 2000
2 changes: 2 additions & 0 deletions src/directives/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import html from './html'
// logic control
import vFor from './for'
import vIf from './if'
import inject from './inject'
import show from './show'
// two-way binding
import model from './model/index'
Expand All @@ -23,6 +24,7 @@ export default {
html,
'for': vFor,
'if': vIf,
inject,
show,
model,
on,
Expand Down
37 changes: 37 additions & 0 deletions src/directives/public/inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import FragmentFactory from '../../fragment/factory'
import { INJECT } from '../priorities'
import {
remove,
createAnchor,
warn
} from '../../util/index'

export default {

priority: INJECT,

bind () {
var el = this.el
if (!el.__vue__) {
var container = this.arg
? document.getElementById(this.arg)
: document.body
this.anchor = createAnchor('v-inject')
container.appendChild(this.anchor)
remove(this.el)
var factory = new FragmentFactory(this.vm, el)
this.frag = factory.create(this._host, this._scope, this._frag)
this.frag.before(this.anchor)
} else {
process.env.NODE_ENV !== 'production' && warn(
'v-inject="' + this.expression + '" cannot be ' +
'used on an instance root element.'
)
}
},

unbind: function () {
this.frag.remove()
remove(this.anchor)
}
}

0 comments on commit 8430843

Please sign in to comment.