Skip to content

Commit 5e638a0

Browse files
committed
methods
1 parent e69bb86 commit 5e638a0

File tree

8 files changed

+40
-43
lines changed

8 files changed

+40
-43
lines changed

src/compiler.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ function Compiler (vm, options) {
5252

5353
// copy data, methods & compiler options
5454
var data = compiler.data = options.data || {}
55-
extend(vm, data, true)
56-
extend(vm, options.methods, true)
5755
extend(compiler, options.compilerOptions)
5856

5957
// initialize element
@@ -92,10 +90,15 @@ function Compiler (vm, options) {
9290
// setup observer
9391
compiler.setupObserver()
9492

95-
// create bindings for computed properties
96-
var computed = options.computed
97-
if (computed) {
98-
for (key in computed) {
93+
// create bindings for computed properties and methods
94+
if (options.methods) {
95+
for (key in options.methods) {
96+
compiler.createBinding(key)
97+
}
98+
}
99+
100+
if (options.computed) {
101+
for (key in options.computed) {
99102
compiler.createBinding(key)
100103
}
101104
}
@@ -105,7 +108,7 @@ function Compiler (vm, options) {
105108
if (params) {
106109
i = params.length
107110
while (i--) {
108-
vm[params[i]] = utils.checkNumber(
111+
data[params[i]] = utils.checkNumber(
109112
compiler.eval(
110113
el.getAttribute(params[i])
111114
)
@@ -118,7 +121,15 @@ function Compiler (vm, options) {
118121

119122
// the user might have set some props on the vm
120123
// so copy it back to the data...
121-
extend(data, vm)
124+
for (key in vm) {
125+
if (typeof vm[key] !== 'function') {
126+
data[key] = vm[key]
127+
}
128+
}
129+
130+
vm.$index = data.$index
131+
vm.$value = data.$value
132+
vm.$key = data.$key
122133

123134
// observe the data
124135
compiler.observeData(data)
@@ -570,15 +581,19 @@ CompilerProto.createBinding = function (key, directive) {
570581
utils.log(' created binding: ' + key)
571582

572583
var compiler = this,
584+
methods = compiler.options.methods,
573585
isExp = directive && directive.isExp,
574-
isFn = directive && directive.isFn,
586+
isFn = (directive && directive.isFn) || (methods && methods[key]),
575587
bindings = compiler.bindings,
576588
computed = compiler.options.computed,
577589
binding = new Binding(compiler, key, isExp, isFn)
578590

579591
if (isExp) {
580592
// expression bindings are anonymous
581593
compiler.defineExp(key, binding, directive)
594+
} else if (isFn) {
595+
bindings[key] = binding
596+
binding.value = compiler.vm[key] = methods[key]
582597
} else {
583598
bindings[key] = binding
584599
if (binding.root) {
@@ -653,9 +668,7 @@ CompilerProto.defineProp = function (key, binding) {
653668
CompilerProto.defineMeta = function (key, binding) {
654669
var vm = this.vm,
655670
ob = this.observer,
656-
value = binding.value = hasOwn.call(vm, key)
657-
? vm[key]
658-
: this.data[key]
671+
value = binding.value = vm[key]
659672
// remove initital meta in data, since the same piece
660673
// of data can be observed by different VMs, each have
661674
// its own associated meta info.

src/main.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,6 @@ function extend (options) {
113113
var proto = ExtendedVM.prototype = Object.create(ParentVM.prototype)
114114
utils.defProtected(proto, 'constructor', ExtendedVM)
115115

116-
// copy prototype props
117-
var methods = options.methods
118-
if (methods) {
119-
for (var key in methods) {
120-
if (
121-
!(key in ViewModel.prototype) &&
122-
typeof methods[key] === 'function'
123-
) {
124-
proto[key] = methods[key]
125-
}
126-
}
127-
}
128-
129116
// allow extended VM to be further extended
130117
ExtendedVM.extend = extend
131118
ExtendedVM.super = ParentVM
@@ -160,7 +147,7 @@ function inheritOptions (child, parent, topLevel) {
160147
child = child || {}
161148
if (!parent) return child
162149
for (var key in parent) {
163-
if (key === 'el' || key === 'methods') continue
150+
if (key === 'el') continue
164151
var val = child[key],
165152
parentVal = parent[key],
166153
type = utils.typeOf(val),

src/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ var utils = module.exports = {
130130
/**
131131
* simple extend
132132
*/
133-
extend: function (obj, ext, protective) {
133+
extend: function (obj, ext) {
134134
for (var key in ext) {
135-
if ((protective && obj[key]) || obj[key] === ext[key]) continue
136-
obj[key] = ext[key]
135+
if (obj[key] !== ext[key]) {
136+
obj[key] = ext[key]
137+
}
137138
}
138139
return obj
139140
},

test/functional/fixtures/events.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</div>
99

1010
<script>
11+
Vue.config({debug:true})
1112
var test = new Vue({
1213
el: 'div',
1314
methods: {

test/functional/fixtures/repeated-items.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<script src="../../../dist/vue.js"></script>
2222
<script>
2323

24-
//Vue.config({debug: true})
24+
Vue.config({debug: true})
2525

2626
var items = [
2727
{ title: 'A'},

test/functional/fixtures/routing.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
<script>
5252

53+
Vue.config('debug', true)
54+
5355
Vue.component('home', {
5456
template: '<h1>Home</h1><div class="content">{{>yield}}</div>',
5557
created: function () {

test/unit/specs/api.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ describe('API', function () {
460460

461461
describe('methods', function () {
462462

463-
it('should be mixed to the exteded VM\'s prototype', function () {
464-
var mixins = {
463+
it('should be mixed to the exteded VM\'s instances', function () {
464+
var methods = {
465465
c: function () {},
466466
d: function () {}
467467
}
468-
var Test = Vue.extend({ methods: mixins })
469-
for (var key in mixins) {
470-
assert.strictEqual(Test.prototype[key], mixins[key])
471-
}
468+
var Test = Vue.extend({ methods: methods })
469+
var t = new Test()
470+
assert.strictEqual(t.c, methods.c)
471+
assert.strictEqual(t.d, methods.d)
472472
})
473473

474474
})

test/unit/specs/utils.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,6 @@ describe('Utils', function () {
151151
assert.strictEqual(a.b, b.b)
152152
})
153153

154-
it('should respect the protective option', function () {
155-
var a = {a: 1}, b = {a: {}, b: 2}
156-
utils.extend(a, b, true)
157-
assert.strictEqual(a.a, 1)
158-
assert.strictEqual(a.b, b.b)
159-
})
160-
161154
it('should always return the extended object', function () {
162155
var a = {a: 1}, b = {a: {}, b: 2}
163156
assert.strictEqual(a, utils.extend(a, b))

0 commit comments

Comments
 (0)