vue2/src/binding.js

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-08-08 23:50:21 +08:00
var Emitter = require('emitter')
2013-08-08 23:45:40 +08:00
/*
* Binding class.
*
* each property on the scope has one corresponding Binding object
* which has multiple directive instances on the DOM
* and multiple computed property dependents
2013-08-08 23:45:40 +08:00
*/
function Binding (seed, key) {
this.seed = seed
this.key = key
this.set(seed.scope[key])
this.defineAccessors(seed, key)
2013-08-08 23:45:40 +08:00
this.instances = []
this.dependents = []
this.dependencies = []
2013-08-08 23:45:40 +08:00
}
/*
* Pre-process a passed in value based on its type
*/
Binding.prototype.set = function (value) {
var type = typeOf(value),
self = this
// preprocess the value depending on its type
if (type === 'Object') {
if (value.get || value.set) { // computed property
2013-08-08 23:45:40 +08:00
self.isComputed = true
} else { // normal object
// TODO watchObject
}
} else if (type === 'Array') {
watchArray(value)
value.on('mutate', function () {
self.emitChange()
})
}
this.value = value
}
/*
* Define getter/setter for this binding on scope
*/
Binding.prototype.defineAccessors = function (seed, key) {
var self = this
Object.defineProperty(seed.scope, key, {
get: function () {
seed.emit('get', key)
return self.isComputed
? self.value.get()
: self.value
},
set: function (value) {
if (self.isComputed && self.value.set) {
self.value.set(value)
} else if (value !== self.value) {
self.value = value
self.update(value)
}
}
})
}
2013-08-08 23:45:40 +08:00
/*
* Process the value, then trigger updates on all dependents
*/
Binding.prototype.update = function (value) {
this.set(value)
this.instances.forEach(function (instance) {
instance.update(value)
})
this.emitChange()
}
/*
2013-08-08 23:50:21 +08:00
* Notify computed properties that depend on this binding
2013-08-08 23:45:40 +08:00
* to update themselves
*/
Binding.prototype.emitChange = function () {
this.dependents.forEach(function (dept) {
dept.refresh()
})
}
/*
* get accurate type of an object
*/
2013-08-08 23:50:21 +08:00
var toString = Object.prototype.toString
2013-08-08 23:45:40 +08:00
function typeOf (obj) {
2013-08-08 23:50:21 +08:00
return toString.call(obj).slice(8, -1)
2013-08-08 23:45:40 +08:00
}
/*
* augment an Array so that it emit events when mutated
*/
2013-08-08 23:50:21 +08:00
var aproto = Array.prototype,
arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
arrayAugmentations = {
2013-08-09 22:23:17 +08:00
remove: function (index) {
if (typeof index !== 'number') index = index.$index
this.splice(index, 1)
2013-08-08 23:50:21 +08:00
},
replace: function (index, data) {
2013-08-09 22:23:17 +08:00
if (typeof index !== 'number') index = index.$index
2013-08-08 23:50:21 +08:00
this.splice(index, 1, data)
2013-08-08 23:45:40 +08:00
}
}
function watchArray (collection) {
Emitter(collection)
arrayMutators.forEach(function (method) {
collection[method] = function () {
2013-08-08 23:50:21 +08:00
var result = aproto[method].apply(this, arguments)
2013-08-08 23:45:40 +08:00
collection.emit('mutate', {
method: method,
2013-08-08 23:50:21 +08:00
args: aproto.slice.call(arguments),
2013-08-08 23:45:40 +08:00
result: result
})
}
})
for (var method in arrayAugmentations) {
collection[method] = arrayAugmentations[method]
}
}
module.exports = Binding