Skip to content

Commit

Permalink
avoid duplicate Observer.convert()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 27, 2013
1 parent 0108e71 commit 331bcc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,25 +470,27 @@ CompilerProto.define = function (key, binding) {
log(' defined root binding: ' + key)

var compiler = this,
data = compiler.data,
vm = compiler.vm,
value = binding.value = data[key] // save the value before redefinening it

if (utils.typeOf(value) === 'Object' && value.$get) {
compiler.markComputed(binding)
}
data = compiler.data,
vm = compiler.vm,
ob = data.__observer__

if (!(key in data)) {
data[key] = undefined
}

// if the data object is already observed, that means
// this binding is created late. we need to observe it now.
if (data.__observer__) {
// if the data object is already observed, but the key
// is not observed, we need to add it to the observed keys.
if (ob && !(key in ob.values)) {
Observer.convert(data, key)
}

var value = binding.value = data[key]
if (utils.typeOf(value) === 'Object' && value.$get) {
compiler.markComputed(binding)
}

Object.defineProperty(vm, key, {
enumerable: !binding.isComputed,
get: binding.isComputed
? function () {
return compiler.data[key].$get()
Expand Down
10 changes: 6 additions & 4 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ function convert (obj, key) {
if ((keyPrefix === '$' || keyPrefix === '_') && key !== '$index') {
return
}
var observer = obj.__observer__,
val = obj[key],
values = observer.values
values[key] = val
// emit set on bind
// this means when an object is observed it will emit
// a first batch of set events.
var observer = obj.__observer__,
values = observer.values,
val = values[key] = obj[key]
observer.emit('set', key, val)
if (Array.isArray(val)) {
observer.emit('set', key + '.length', val.length)
}
Object.defineProperty(obj, key, {
get: function () {
var value = values[key]
Expand Down

0 comments on commit 331bcc6

Please sign in to comment.