make prop bindings immutable

This commit is contained in:
Evan You 2016-04-14 17:22:06 -04:00
parent 7c3fa3c2bb
commit 56073ca853
5 changed files with 28 additions and 20 deletions

View File

@ -23,7 +23,7 @@ Vue.prototype.$mount = function (el) {
} else {
template = getOuterHTML(query(el))
}
options.render = new Function(compile(template, config.preserveWhiteSpace))
options.render = new Function(compile(template, config.preserveWhitespace))
}
mount.call(this, el)
}

View File

@ -4,7 +4,7 @@ export default {
* Preserve whitespaces between elements.
*/
preserveWhiteSpace: true,
preserveWhitespace: true,
/**
* Whether to suppress warnings.

View File

@ -77,14 +77,16 @@ export function renderMixin (Vue) {
this.$forceUpdate()
return
}
// set props. because props are reactive,
// if any of them has changed it will trigger an update.
// set props if they have changed.
const attrs = data && data.attrs
if (attrs) {
for (let key in this.$options.props) {
this[key] = hasOwn(attrs, key)
let newVal = hasOwn(attrs, key)
? attrs[key]
: attrs[hyphenate(key)]
if (this[key] !== newVal) {
this[key] = newVal
}
}
}
}
@ -99,7 +101,6 @@ export function renderMixin (Vue) {
mergeParentData(this, data, parentData)
}
renderState.activeInstance = prev
console.log(vnode)
return vnode
}

View File

@ -29,7 +29,7 @@ function initProps (vm) {
if (props) {
withoutConversion(() => {
for (let key in props) {
defineReactive(vm, key, attrs[key])
defineReactive(vm, key, attrs[key], true /* immutable */)
}
})
}

View File

@ -6,7 +6,8 @@ import {
isObject,
isPlainObject,
hasProto,
hasOwn
hasOwn,
warn
} from '../util/index'
const arrayKeys = Object.getOwnPropertyNames(arrayMethods)
@ -192,7 +193,7 @@ export function observe (value, vm) {
* @param {*} val
*/
export function defineReactive (obj, key, val) {
export function defineReactive (obj, key, val, immutable) {
var dep = new Dep()
var property = Object.getOwnPropertyDescriptor(obj, key)
@ -224,18 +225,24 @@ export function defineReactive (obj, key, val) {
}
return value
},
set: function reactiveSetter (newVal) {
var value = getter ? getter.call(obj) : val
if (newVal === value) {
return
set: immutable
? function immutableSetter () {
if (process.env.NODE_ENV !== 'production') {
warn(`property "${key}" is immutable.`)
}
}
if (setter) {
setter.call(obj, newVal)
} else {
val = newVal
: function reactiveSetter (newVal) {
var value = getter ? getter.call(obj) : val
if (newVal === value) {
return
}
if (setter) {
setter.call(obj, newVal)
} else {
val = newVal
}
childOb = observe(newVal)
dep.notify()
}
childOb = observe(newVal)
dep.notify()
}
})
}