Skip to content

Commit 35967cb

Browse files
committed
vm.$set no longer need to check owner VM at run time
1 parent 31f3d65 commit 35967cb

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

src/directives/model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
tag = el.tagName
2525

2626
self.lock = false
27+
self.ownerVM = self.binding.compiler.vm
2728

2829
// determine what event to listen to
2930
self.event =
@@ -111,7 +112,7 @@ module.exports = {
111112
},
112113

113114
_set: function () {
114-
this.vm.$set(
115+
this.ownerVM.$set(
115116
this.key, this.multi
116117
? getMultipleSelectOptions(this.el)
117118
: this.el[this.attr]

src/viewmodel.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ var VMProto = ViewModel.prototype
2424
*/
2525
def(VMProto, '$set', function (key, value) {
2626
var path = key.split('.'),
27-
obj = getTargetVM(this, path)
28-
if (!obj) return
27+
obj = this
2928
for (var d = 0, l = path.length - 1; d < l; d++) {
3029
obj = obj[path[d]]
3130
}
@@ -159,16 +158,4 @@ function query (el) {
159158
: el
160159
}
161160

162-
/**
163-
* If a VM doesn't contain a path, go up the prototype chain
164-
* to locate the ancestor that has it.
165-
*/
166-
function getTargetVM (vm, path) {
167-
var baseKey = path[0],
168-
binding = vm.$compiler.bindings[baseKey]
169-
return binding
170-
? binding.compiler.vm
171-
: null
172-
}
173-
174161
module.exports = ViewModel

test/unit/specs/directives.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('UNIT: Directives', function () {
215215
it('should trigger vm.$set when clicked', function () {
216216
var triggered = false
217217
dir.key = 'foo'
218-
dir.vm = { $set: function (key, val) {
218+
dir.ownerVM = { $set: function (key, val) {
219219
assert.strictEqual(key, 'foo')
220220
assert.strictEqual(val, true)
221221
triggered = true
@@ -226,8 +226,10 @@ describe('UNIT: Directives', function () {
226226

227227
it('should remove event listener with unbind()', function () {
228228
var removed = true
229-
dir.vm.$set = function () {
230-
removed = false
229+
dir.ownerVM = {
230+
$set: function () {
231+
removed = false
232+
}
231233
}
232234
dir.unbind()
233235
dir.el.dispatchEvent(mockMouseEvent('click'))
@@ -265,7 +267,7 @@ describe('UNIT: Directives', function () {
265267
it('should trigger vm.$set when clicked', function () {
266268
var triggered = false
267269
dir2.key = 'radio'
268-
dir2.vm = { $set: function (key, val) {
270+
dir2.ownerVM = { $set: function (key, val) {
269271
triggered = true
270272
assert.strictEqual(key, 'radio')
271273
assert.strictEqual(val, dir2.el.value)
@@ -278,7 +280,7 @@ describe('UNIT: Directives', function () {
278280

279281
it('should remove listeners on unbind()', function () {
280282
var removed = true
281-
dir1.vm = { $set: function () {
283+
dir1.ownerVM = { $set: function () {
282284
removed = false
283285
}}
284286
dir1.unbind()
@@ -316,7 +318,7 @@ describe('UNIT: Directives', function () {
316318
it('should trigger vm.$set when value is changed', function () {
317319
var triggered = false
318320
dir.key = 'select'
319-
dir.vm = { $set: function (key, val) {
321+
dir.ownerVM = { $set: function (key, val) {
320322
triggered = true
321323
assert.strictEqual(key, 'select')
322324
assert.equal(val, 1)
@@ -328,7 +330,7 @@ describe('UNIT: Directives', function () {
328330

329331
it('should remove listener on unbind()', function () {
330332
var removed = true
331-
dir.vm = { $set: function () {
333+
dir.ownerVM = { $set: function () {
332334
removed = false
333335
}}
334336
dir.unbind()
@@ -360,7 +362,7 @@ describe('UNIT: Directives', function () {
360362
it('should trigger vm.$set when value is changed via input', function () {
361363
var triggered = false
362364
dir.key = 'foo'
363-
dir.vm = { $set: function (key, val) {
365+
dir.ownerVM = { $set: function (key, val) {
364366
assert.ok(dir.lock, 'the directive should be locked if it has no filters')
365367
assert.strictEqual(key, 'foo')
366368
assert.strictEqual(val, 'bar')
@@ -373,8 +375,10 @@ describe('UNIT: Directives', function () {
373375

374376
it('should remove event listener with unbind()', function () {
375377
var removed = true
376-
dir.vm.$set = function () {
377-
removed = false
378+
dir.ownerVM = {
379+
$set: function () {
380+
removed = false
381+
}
378382
}
379383
dir.unbind()
380384
dir.el.dispatchEvent(mockHTMLEvent('input'))
@@ -386,7 +390,7 @@ describe('UNIT: Directives', function () {
386390
var dir = mockDirective('model', 'input', 'text')
387391
dir.filters = []
388392
dir.bind()
389-
dir.vm = {$set:function () {
393+
dir.ownerVM = {$set:function () {
390394
assert.notOk(dir.lock)
391395
triggered = true
392396
}}

0 commit comments

Comments
 (0)