From 3d6b5fc935868a11d41faa0f486ff6aa0326732b Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 11 Apr 2016 00:39:55 -0400 Subject: [PATCH] fix things --- src/instance/index.js | 77 ++++++++++++++++++--------------------- src/vdom/modules/attrs.js | 2 +- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/instance/index.js b/src/instance/index.js index d0004a63..722fdf3b 100644 --- a/src/instance/index.js +++ b/src/instance/index.js @@ -4,52 +4,45 @@ import Watcher from '../observer/watcher' import { h, patch } from '../vdom/index' import { nextTick, isReserved, getOuterHTML } from '../util/index' -export default class Component { - constructor (options) { - this.$options = options - this._data = options.data - const el = this._el = document.querySelector(options.el) - const render = compile(getOuterHTML(el)) - this._el.innerHTML = '' - Object.keys(options.data).forEach(key => this._proxy(key)) - if (options.methods) { - Object.keys(options.methods).forEach(key => { - this[key] = options.methods[key].bind(this) - }) - } - this._ob = observe(options.data) - this._watchers = [] - this._watcher = new Watcher(this, render, this._update) - this._update(this._watcher.value) +export default function Component (options) { + this.$options = options + this._data = options.data + const el = this._el = document.querySelector(options.el) + const render = compile(getOuterHTML(el)) + this._el.innerHTML = '' + Object.keys(options.data).forEach(key => proxy(this, key)) + if (options.methods) { + Object.keys(options.methods).forEach(key => { + this[key] = options.methods[key].bind(this) + }) } + this._ob = observe(options.data) + this._watchers = [] + this._watcher = new Watcher(this, render, this._update) + this._update(this._watcher.value) +} - _update (vtree) { - if (!this._tree) { - patch(this._el, vtree) - } else { - patch(this._tree, vtree) - } - this._tree = vtree +Component.prototype._update = function (vtree) { + if (!this._tree) { + patch(this._el, vtree) + } else { + patch(this._tree, vtree) } + this._tree = vtree +} - _proxy (key) { - if (!isReserved(key)) { - // need to store ref to self here - // because these getter/setters might - // be called by child scopes via - // prototype inheritance. - var self = this - Object.defineProperty(self, key, { - configurable: true, - enumerable: true, - get: function proxyGetter () { - return self._data[key] - }, - set: function proxySetter (val) { - self._data[key] = val - } - }) - } +function proxy (vm, key) { + if (!isReserved(key)) { + Object.defineProperty(vm, key, { + configurable: true, + enumerable: true, + get: function proxyGetter () { + return vm._data[key] + }, + set: function proxySetter (val) { + vm._data[key] = val + } + }) } } diff --git a/src/vdom/modules/attrs.js b/src/vdom/modules/attrs.js index 71a55ca6..63ace116 100644 --- a/src/vdom/modules/attrs.js +++ b/src/vdom/modules/attrs.js @@ -12,7 +12,7 @@ for (let i = 0, len = booleanAttrs.length; i < len; i++) { booleanAttrsDict[booleanAttrs[i]] = true } -function updateAttr (oldVnode, vnode) { +function updateAttrs (oldVnode, vnode) { let key, cur, old const elm = vnode.elm const oldAttrs = oldVnode.data.attrs || {}