Skip to content

Commit 6204f8b

Browse files
committed
use simple bind instead of native bind, remove vm.$get()
1 parent b4902ae commit 6204f8b

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

src/compiler.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,12 @@ CompilerProto.markComputed = function (binding) {
507507
binding.isComputed = true
508508
// bind the accessors to the vm
509509
if (binding.isFn) {
510-
binding.value = value.bind(vm)
510+
binding.value = utils.bind(value, vm)
511511
} else {
512-
value.$get = value.$get.bind(vm)
513-
if (value.$set) value.$set = value.$set.bind(vm)
512+
value.$get = utils.bind(value.$get, vm)
513+
if (value.$set) {
514+
value.$set = utils.bind(value.$set, vm)
515+
}
514516
}
515517
// keep track for dep parsing later
516518
this.computed.push(binding)

src/directive.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ DirProto.refresh = function (value) {
148148
if (this.isFn) {
149149
value = this.value
150150
} else {
151-
value = this.value.$get({
152-
el: this.el,
153-
vm: this.vm
154-
})
151+
value = this.value.$get()
155152
if (value !== undefined && value === this.computedValue) return
156153
this.computedValue = value
157154
}

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ var utils = module.exports = {
5555
return toString.call(obj).slice(8, -1)
5656
},
5757

58+
/**
59+
* Most simple bind with no arguments
60+
* enough for the usecase and fast than native bind()
61+
*/
62+
bind: function (fn, ctx) {
63+
return function () {
64+
return fn.call(ctx)
65+
}
66+
},
67+
5868
/**
5969
* Make sure only strings and numbers are output to html
6070
* output empty string is value is not string or number

src/viewmodel.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ def(VMProto, '$set', function (key, value) {
2929
obj[path[d]] = value
3030
})
3131

32-
/**
33-
* The function for getting a key
34-
* which will go up along the prototype chain of the bindings
35-
* Used in exp-parser.
36-
*/
37-
def(VMProto, '$get', function (key) {
38-
var path = key.split('.'),
39-
obj = getTargetVM(this, path),
40-
vm = obj
41-
if (!obj) return
42-
for (var d = 0, l = path.length; d < l; d++) {
43-
obj = obj[path[d]]
44-
}
45-
if (typeof obj === 'function') obj = obj.bind(vm)
46-
return obj
47-
})
48-
4932
/**
5033
* watch a key on the viewmodel for changes
5134
* fire callback with new value

test/unit/specs/directive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ describe('UNIT: Directive', function () {
285285
applied = false,
286286
el = 1, vm = 2,
287287
value = {
288-
$get: function (ctx) {
289-
return ctx.el + ctx.vm
288+
$get: function () {
289+
return el + vm
290290
}
291291
}
292292
d.el = el

test/unit/specs/viewmodel.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ describe('UNIT: ViewModel', function () {
1414
b: arr
1515
}
1616
})
17-
18-
describe('.$get()', function () {
19-
it('should retrieve correct value', function () {
20-
assert.strictEqual(vm.$get('a.b.c'), data.b.c)
21-
})
22-
})
2317

2418
describe('.$set()', function () {
2519
vm.$set('a.b.c', 54321)

0 commit comments

Comments
 (0)