Skip to content

Commit f139f92

Browse files
committed
simplify binding mechanism, remove refresh()
1 parent 3f8a3cb commit f139f92

File tree

5 files changed

+22
-67
lines changed

5 files changed

+22
-67
lines changed

src/batcher.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
var config = require('./config'),
2-
utils = require('./utils'),
1+
var utils = require('./utils'),
32
queue, has, waiting
43

54
reset()
65

7-
exports.queue = function (binding, method) {
8-
if (!config.async) {
9-
binding['_' + method]()
10-
return
11-
}
6+
exports.queue = function (binding) {
127
if (!has[binding.id]) {
13-
queue.push({
14-
binding: binding,
15-
method: method
16-
})
8+
queue.push(binding)
179
has[binding.id] = true
1810
if (!waiting) {
1911
waiting = true
@@ -24,10 +16,9 @@ exports.queue = function (binding, method) {
2416

2517
function flush () {
2618
for (var i = 0; i < queue.length; i++) {
27-
var task = queue[i],
28-
b = task.binding
19+
var b = queue[i]
2920
if (b.unbound) continue
30-
b['_' + task.method]()
21+
b._update()
3122
has[b.id] = false
3223
}
3324
reset()

src/binding.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,33 @@ function Binding (compiler, key, isExp, isFn) {
2525
var BindingProto = Binding.prototype
2626

2727
/**
28-
* Process the value, then trigger updates on all dependents
28+
* Update value and queue instance updates.
2929
*/
3030
BindingProto.update = function (value) {
31-
this.value = value
32-
batcher.queue(this, 'update')
31+
if (arguments.length) this.value = value
32+
batcher.queue(this)
3333
}
3434

35+
/**
36+
* Actually update the instances.
37+
*/
3538
BindingProto._update = function () {
36-
var i = this.instances.length
39+
var i = this.instances.length,
40+
value = this.eval()
3741
while (i--) {
38-
this.instances[i].update(this.value)
42+
this.instances[i].update(value)
3943
}
4044
this.pub()
4145
}
4246

4347
/**
44-
* -- computed property only --
45-
* Force all instances to re-evaluate themselves
48+
* Return the valuated value regardless
49+
* of whether it is computed or not
4650
*/
47-
BindingProto.refresh = function () {
48-
batcher.queue(this, 'refresh')
49-
}
50-
51-
BindingProto._refresh = function () {
52-
var i = this.instances.length
53-
while (i--) {
54-
this.instances[i].refresh()
55-
}
56-
this.pub()
51+
BindingProto.eval = function () {
52+
return this.isComputed && !this.isFn
53+
? this.value.$get()
54+
: this.value
5755
}
5856

5957
/**
@@ -63,7 +61,7 @@ BindingProto._refresh = function () {
6361
BindingProto.pub = function () {
6462
var i = this.subs.length
6563
while (i--) {
66-
this.subs[i].refresh()
64+
this.subs[i].update()
6765
}
6866
}
6967

src/compiler.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,7 @@ CompilerProto.bindDirective = function (directive) {
425425
}
426426

427427
// set initial value
428-
var value = binding.value
429-
if (value !== undefined) {
430-
if (binding.isComputed) {
431-
directive.refresh(value)
432-
} else {
433-
directive.update(value, true)
434-
}
435-
}
428+
directive.update(binding.eval(), true)
436429
}
437430

438431
/**

src/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var prefix = 'v',
1111
],
1212
config = module.exports = {
1313

14-
async : true,
1514
debug : false,
1615
silent : false,
1716
enterClass : 'v-enter',

src/directive.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,6 @@ function parseFilter (filter, compiler) {
123123
DirProto.update = function (value, init) {
124124
if (!init && value === this.value) return
125125
this.value = value
126-
this.apply(value)
127-
}
128-
129-
/**
130-
* -- computed property only --
131-
* called when a dependency has changed
132-
*/
133-
DirProto.refresh = function (value) {
134-
// pass element and viewmodel info to the getter
135-
// enables context-aware bindings
136-
if (value) this.value = value
137-
138-
if (this.isFn) {
139-
value = this.value
140-
} else {
141-
value = this.value.$get()
142-
if (value !== undefined && value === this.computedValue) return
143-
this.computedValue = value
144-
}
145-
this.apply(value)
146-
}
147-
148-
/**
149-
* Actually invoking the _update from the directive's definition
150-
*/
151-
DirProto.apply = function (value) {
152126
if (this._update) {
153127
this._update(
154128
this.filters

0 commit comments

Comments
 (0)