Skip to content

Commit 894b5fd

Browse files
author
Evan You
committed
make methods react to changes
1 parent 3be8ff5 commit 894b5fd

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

src/compiler.js

+27-25
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,6 @@ function Compiler (vm, options) {
6868
compiler.children = []
6969
compiler.emitter = new Emitter(vm)
7070

71-
// create bindings for computed properties
72-
if (options.methods) {
73-
for (key in options.methods) {
74-
compiler.createBinding(key)
75-
}
76-
}
77-
78-
// create bindings for methods
79-
if (options.computed) {
80-
for (key in options.computed) {
81-
compiler.createBinding(key)
82-
}
83-
}
84-
8571
// VM ---------------------------------------------------------------------
8672

8773
// set VM properties
@@ -110,6 +96,20 @@ function Compiler (vm, options) {
11096
// this is necesarry for all hooks and data observation events
11197
compiler.setupObserver()
11298

99+
// create bindings for computed properties
100+
if (options.methods) {
101+
for (key in options.methods) {
102+
compiler.createBinding(key)
103+
}
104+
}
105+
106+
// create bindings for methods
107+
if (options.computed) {
108+
for (key in options.computed) {
109+
compiler.createBinding(key)
110+
}
111+
}
112+
113113
// initialize data
114114
var data = compiler.data = options.data || {},
115115
defaultData = options.defaultData
@@ -719,7 +719,7 @@ CompilerProto.createBinding = function (key, directive) {
719719
compiler.defineExp(key, binding, directive)
720720
} else if (isFn) {
721721
bindings[key] = binding
722-
binding.value = compiler.vm[key] = methods[key]
722+
compiler.defineVmProp(key, binding, methods[key])
723723
} else {
724724
bindings[key] = binding
725725
if (binding.root) {
@@ -729,9 +729,12 @@ CompilerProto.createBinding = function (key, directive) {
729729
compiler.defineComputed(key, binding, computed[key])
730730
} else if (key.charAt(0) !== '$') {
731731
// normal property
732-
compiler.defineProp(key, binding)
732+
compiler.defineDataProp(key, binding)
733733
} else {
734-
compiler.defineMeta(key, binding)
734+
// properties that start with $ are meta properties
735+
// they should be kept on the vm but not in the data object.
736+
compiler.defineVmProp(key, binding, compiler.data[key])
737+
delete compiler.data[key]
735738
}
736739
} else if (computed && computed[utils.baseKey(key)]) {
737740
// nested path on computed property
@@ -753,10 +756,10 @@ CompilerProto.createBinding = function (key, directive) {
753756
}
754757

755758
/**
756-
* Define the getter/setter for a root-level property on the VM
757-
* and observe the initial value
759+
* Define the getter/setter to proxy a root-level
760+
* data property on the VM
758761
*/
759-
CompilerProto.defineProp = function (key, binding) {
762+
CompilerProto.defineDataProp = function (key, binding) {
760763
var compiler = this,
761764
data = compiler.data,
762765
ob = data.__emitter__
@@ -786,14 +789,13 @@ CompilerProto.defineProp = function (key, binding) {
786789
}
787790

788791
/**
789-
* Define a meta property, e.g. $index or $key,
790-
* which is bindable but only accessible on the VM,
792+
* Define a vm property, e.g. $index, $key, or mixin methods
793+
* which are bindable but only accessible on the VM,
791794
* not in the data.
792795
*/
793-
CompilerProto.defineMeta = function (key, binding) {
796+
CompilerProto.defineVmProp = function (key, binding, value) {
794797
var ob = this.observer
795-
binding.value = this.data[key]
796-
delete this.data[key]
798+
binding.value = value
797799
def(this.vm, key, {
798800
get: function () {
799801
if (Observer.shouldGet) ob.emit('get', key)

test/unit/specs/api.js

+23
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,29 @@ describe('API', function () {
470470
assert.strictEqual(t.d, methods.d)
471471
})
472472

473+
it('should be bindable like normal properties', function (done) {
474+
var Test = Vue.extend({
475+
template: '{{ go(msg) }}',
476+
data: {
477+
msg: 'ok'
478+
},
479+
methods: {
480+
go: function (v) {
481+
return v + ' before'
482+
}
483+
}
484+
})
485+
var vm = new Test()
486+
assert.equal(vm.$el.textContent, 'ok before')
487+
vm.go = function (v) {
488+
return v + ' after'
489+
}
490+
nextTick(function () {
491+
assert.equal(vm.$el.textContent, 'ok after')
492+
done()
493+
})
494+
})
495+
473496
})
474497

475498
describe('data', function () {

0 commit comments

Comments
 (0)