Skip to content

Commit

Permalink
simplify binding mechanism, remove refresh()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 26, 2014
1 parent 3f8a3cb commit f139f92
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 67 deletions.
19 changes: 5 additions & 14 deletions src/batcher.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
var config = require('./config'),
utils = require('./utils'),
var utils = require('./utils'),
queue, has, waiting

reset()

exports.queue = function (binding, method) {
if (!config.async) {
binding['_' + method]()
return
}
exports.queue = function (binding) {
if (!has[binding.id]) {
queue.push({
binding: binding,
method: method
})
queue.push(binding)
has[binding.id] = true
if (!waiting) {
waiting = true
Expand All @@ -24,10 +16,9 @@ exports.queue = function (binding, method) {

function flush () {
for (var i = 0; i < queue.length; i++) {
var task = queue[i],
b = task.binding
var b = queue[i]
if (b.unbound) continue
b['_' + task.method]()
b._update()
has[b.id] = false
}
reset()
Expand Down
34 changes: 16 additions & 18 deletions src/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,33 @@ function Binding (compiler, key, isExp, isFn) {
var BindingProto = Binding.prototype

/**
* Process the value, then trigger updates on all dependents
* Update value and queue instance updates.
*/
BindingProto.update = function (value) {
this.value = value
batcher.queue(this, 'update')
if (arguments.length) this.value = value
batcher.queue(this)
}

/**
* Actually update the instances.
*/
BindingProto._update = function () {
var i = this.instances.length
var i = this.instances.length,
value = this.eval()
while (i--) {
this.instances[i].update(this.value)
this.instances[i].update(value)
}
this.pub()
}

/**
* -- computed property only --
* Force all instances to re-evaluate themselves
* Return the valuated value regardless
* of whether it is computed or not
*/
BindingProto.refresh = function () {
batcher.queue(this, 'refresh')
}

BindingProto._refresh = function () {
var i = this.instances.length
while (i--) {
this.instances[i].refresh()
}
this.pub()
BindingProto.eval = function () {
return this.isComputed && !this.isFn
? this.value.$get()
: this.value
}

/**
Expand All @@ -63,7 +61,7 @@ BindingProto._refresh = function () {
BindingProto.pub = function () {
var i = this.subs.length
while (i--) {
this.subs[i].refresh()
this.subs[i].update()
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,7 @@ CompilerProto.bindDirective = function (directive) {
}

// set initial value
var value = binding.value
if (value !== undefined) {
if (binding.isComputed) {
directive.refresh(value)
} else {
directive.update(value, true)
}
}
directive.update(binding.eval(), true)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var prefix = 'v',
],
config = module.exports = {

async : true,
debug : false,
silent : false,
enterClass : 'v-enter',
Expand Down
26 changes: 0 additions & 26 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,6 @@ function parseFilter (filter, compiler) {
DirProto.update = function (value, init) {
if (!init && value === this.value) return
this.value = value
this.apply(value)
}

/**
* -- computed property only --
* called when a dependency has changed
*/
DirProto.refresh = function (value) {
// pass element and viewmodel info to the getter
// enables context-aware bindings
if (value) this.value = value

if (this.isFn) {
value = this.value
} else {
value = this.value.$get()
if (value !== undefined && value === this.computedValue) return
this.computedValue = value
}
this.apply(value)
}

/**
* Actually invoking the _update from the directive's definition
*/
DirProto.apply = function (value) {
if (this._update) {
this._update(
this.filters
Expand Down

0 comments on commit f139f92

Please sign in to comment.