support template option

This commit is contained in:
Evan You 2016-04-12 22:51:49 -04:00
parent 314f25eaf3
commit 0e1ac07919
5 changed files with 69 additions and 49 deletions

View File

@ -1,16 +1,33 @@
import config from './runtime/config'
import { compile } from './compiler/index'
import { getOuterHTML, extend, query } from './runtime/util/index'
import Instance from './runtime/index'
import { getOuterHTML, extend, query, warn } from './runtime/util/index'
import Vue from './runtime/index'
export default function Vue (options) {
const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (el) {
const options = this.$options
// resolve template/el and convert to render function
if (!options.render) {
const template = options.template || getOuterHTML(query(options.el))
let template = options.template
if (template) {
if (typeof template === 'string') {
if (template.charAt(0) === '#') {
template = query(template).innerHTML
}
} else if (template.nodeType) {
template = template.innerHTML
} else {
warn('invalid template option:' + template, this)
}
} else {
template = getOuterHTML(query(el))
}
options.render = compile(template, config.preserveWhiteSpace)
}
return new Instance(options)
mount.call(this, el)
}
extend(Vue, Instance)
Vue.compile = compile
export default Vue

View File

@ -1,6 +1,13 @@
import Vue from './instance/index'
import { nextTick } from './util/index'
Vue.options = {
directives: Object.create(null),
filters: Object.create(null),
components: Object.create(null),
transitions: Object.create(null)
}
Vue.nextTick = nextTick
Vue.version = '2.0.0'

View File

@ -2,10 +2,32 @@ import { initState, stateMixin } from './state'
import { initRender, renderMixin } from './render'
import { initEvents, eventsMixin } from './events'
import { initLifecycle, lifecycleMixin } from './lifecycle'
import { nextTick } from '../util/index'
import { nextTick, mergeOptions } from '../util/index'
let uid = 0
export default function Vue (options) {
this.$options = options
options = options || {}
this.$parent = options.parent
this.$root = this.$parent
? this.$parent.$root
: this
this.$children = []
this.$refs = {} // child vm references
this.$els = {} // element references
// a uid
this._uid = uid++
// a flag to avoid this being observed
this._isVue = true
options = this.$options = mergeOptions(
this.constructor.options,
options,
this
)
initState(this)
initEvents(this)
initLifecycle(this)

View File

@ -3,11 +3,10 @@ import { query } from '../util/index'
import { h, patch } from '../vdom/index'
export function initRender (vm) {
const options = vm.$options
vm._el = query(options.el)
vm._el.innerHTML = ''
vm._watcher = new Watcher(vm, options.render, vm._update)
vm._update(vm._watcher.value)
const el = vm.$options.el
if (el) {
vm.$mount(el)
}
}
export function renderMixin (Vue) {
@ -15,13 +14,20 @@ export function renderMixin (Vue) {
Vue.prototype._update = function (vtree) {
if (!this._tree) {
patch(this._el, vtree)
patch(this.$el, vtree)
} else {
patch(this._tree, vtree)
}
this._tree = vtree
}
Vue.prototype.$mount = function (el) {
this.$el = el ? query(el) : document.createElement('div')
this.$el.innerHTML = ''
this._watcher = new Watcher(this, this.$options.render, this._update)
this._update(this._watcher.value)
}
Vue.prototype.$forceUpdate = function () {
this._watcher.run()
}

View File

@ -153,7 +153,7 @@ strats.activate = function (parentVal, childVal) {
function mergeAssets (parentVal, childVal) {
var res = Object.create(parentVal)
return childVal
? extend(res, guardArrayAssets(childVal))
? extend(res, childVal)
: res
}
@ -221,8 +221,7 @@ var defaultStrat = function (parentVal, childVal) {
function guardComponents (options) {
if (options.components) {
var components = options.components =
guardArrayAssets(options.components)
var components = options.components
var ids = Object.keys(components)
var def
if (process.env.NODE_ENV !== 'production') {
@ -283,37 +282,6 @@ function guardProps (options) {
}
}
/**
* Guard an Array-format assets option and converted it
* into the key-value Object format.
*
* @param {Object|Array} assets
* @return {Object}
*/
function guardArrayAssets (assets) {
if (isArray(assets)) {
var res = {}
var i = assets.length
var asset
while (i--) {
asset = assets[i]
var id = typeof asset === 'function'
? ((asset.options && asset.options.name) || asset.id)
: (asset.name || asset.id)
if (!id) {
process.env.NODE_ENV !== 'production' && warn(
'Array-syntax assets must provide a "name" or "id" field.'
)
} else {
res[id] = asset
}
}
return res
}
return assets
}
/**
* Merge two option objects into a new one.
* Core utility used in both instantiation and inheritance.