restructure instance

This commit is contained in:
Evan You 2016-04-12 21:41:12 -04:00
parent 91a04ad038
commit b5800b274c
7 changed files with 64 additions and 35 deletions

View File

@ -1,5 +1,7 @@
import Vue from './instance/index'
import { nextTick } from './util/index'
Vue.nextTick = nextTick
Vue.version = '2.0.0'
export default Vue

View File

@ -0,0 +1,3 @@
export function apiMixin (Vue) {
}

View File

@ -0,0 +1,7 @@
export function initEvents (vm) {
}
export function eventsMixin (Vue) {
}

View File

@ -1,41 +1,17 @@
import Watcher from '../observer/watcher'
import { h, patch } from '../vdom/index'
import { nextTick, query } from '../util/index'
import { initState, setData } from './state'
import { initState, stateMixin } from './state'
import { initRender, renderMixin } from './render'
import { initEvents, eventsMixin } from './events'
import { apiMixin } from './api'
export default function Vue (options) {
this.$options = options
this._watchers = []
initState(this)
this._el = query(options.el)
this._el.innerHTML = ''
this._watcher = new Watcher(this, options.render, this._update)
this._update(this._watcher.value)
initEvents(this)
initRender(this)
}
Vue.prototype._update = function (vtree) {
if (!this._tree) {
patch(this._el, vtree)
} else {
patch(this._tree, vtree)
}
this._tree = vtree
}
Vue.prototype.$forceUpdate = function () {
this._watcher.run()
}
Object.defineProperty(Vue.prototype, '$data', {
get () {
return this._data
},
set (newData) {
if (newData !== this._data) {
setData(this, newData)
}
}
})
Vue.prototype.__h__ = h
Vue.nextTick = nextTick
stateMixin(Vue)
eventsMixin(Vue)
renderMixin(Vue)
apiMixin(Vue)

View File

@ -0,0 +1,28 @@
import Watcher from '../observer/watcher'
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)
}
export function renderMixin (Vue) {
Vue.prototype.__h__ = h
Vue.prototype._update = function (vtree) {
if (!this._tree) {
patch(this._el, vtree)
} else {
patch(this._tree, vtree)
}
this._tree = vtree
}
Vue.prototype.$forceUpdate = function () {
this._watcher.run()
}
}

View File

@ -112,7 +112,20 @@ function unproxy (vm, key) {
}
}
export function setData (vm, newData) {
export function stateMixin (Vue) {
Object.defineProperty(Vue.prototype, '$data', {
get () {
return this._data
},
set (newData) {
if (newData !== this._data) {
setData(this, newData)
}
}
})
}
function setData (vm, newData) {
newData = newData || {}
var oldData = vm._data
vm._data = newData