Browse Source

restructure instance

dev
Evan You 9 years ago
parent
commit
b5800b274c
  1. 2
      src/runtime/index.js
  2. 3
      src/runtime/instance/api.js
  3. 7
      src/runtime/instance/events.js
  4. 44
      src/runtime/instance/index.js
  5. 0
      src/runtime/instance/init.js
  6. 28
      src/runtime/instance/render.js
  7. 15
      src/runtime/instance/state.js

2
src/runtime/index.js

@ -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

3
src/runtime/instance/api.js

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

7
src/runtime/instance/events.js

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

44
src/runtime/instance/index.js

@ -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)

0
src/runtime/instance/init.js

28
src/runtime/instance/render.js

@ -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()
}
}

15
src/runtime/instance/state.js

@ -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

Loading…
Cancel
Save