Просмотр исходного кода

vm.$set no longer need to check owner VM at run time

Evan You 12 лет назад
Родитель
Сommit
35967cba5a
3 измененных файлов с 18 добавлено и 26 удалено
  1. 2 1
      src/directives/model.js
  2. 1 14
      src/viewmodel.js
  3. 15 11
      test/unit/specs/directives.js

+ 2 - 1
src/directives/model.js

@@ -24,6 +24,7 @@ module.exports = {
             tag  = el.tagName
 
         self.lock = false
+        self.ownerVM = self.binding.compiler.vm
 
         // determine what event to listen to
         self.event =
@@ -111,7 +112,7 @@ module.exports = {
     },
 
     _set: function () {
-        this.vm.$set(
+        this.ownerVM.$set(
             this.key, this.multi
                 ? getMultipleSelectOptions(this.el)
                 : this.el[this.attr]

+ 1 - 14
src/viewmodel.js

@@ -24,8 +24,7 @@ var VMProto = ViewModel.prototype
  */
 def(VMProto, '$set', function (key, value) {
     var path = key.split('.'),
-        obj = getTargetVM(this, path)
-    if (!obj) return
+        obj = this
     for (var d = 0, l = path.length - 1; d < l; d++) {
         obj = obj[path[d]]
     }
@@ -159,16 +158,4 @@ function query (el) {
         : el
 }
 
-/**
- *  If a VM doesn't contain a path, go up the prototype chain
- *  to locate the ancestor that has it.
- */
-function getTargetVM (vm, path) {
-    var baseKey = path[0],
-        binding = vm.$compiler.bindings[baseKey]
-    return binding
-        ? binding.compiler.vm
-        : null
-}
-
 module.exports = ViewModel

+ 15 - 11
test/unit/specs/directives.js

@@ -215,7 +215,7 @@ describe('UNIT: Directives', function () {
             it('should trigger vm.$set when clicked', function () {
                 var triggered = false
                 dir.key = 'foo'
-                dir.vm = { $set: function (key, val) {
+                dir.ownerVM = { $set: function (key, val) {
                     assert.strictEqual(key, 'foo')
                     assert.strictEqual(val, true)
                     triggered = true
@@ -226,8 +226,10 @@ describe('UNIT: Directives', function () {
 
             it('should remove event listener with unbind()', function () {
                 var removed = true
-                dir.vm.$set = function () {
-                    removed = false
+                dir.ownerVM = {
+                    $set: function () {
+                       removed = false
+                    }
                 }
                 dir.unbind()
                 dir.el.dispatchEvent(mockMouseEvent('click'))
@@ -265,7 +267,7 @@ describe('UNIT: Directives', function () {
             it('should trigger vm.$set when clicked', function () {
                 var triggered = false
                 dir2.key = 'radio'
-                dir2.vm = { $set: function (key, val) {
+                dir2.ownerVM = { $set: function (key, val) {
                     triggered = true
                     assert.strictEqual(key, 'radio')
                     assert.strictEqual(val, dir2.el.value)
@@ -278,7 +280,7 @@ describe('UNIT: Directives', function () {
 
             it('should remove listeners on unbind()', function () {
                 var removed = true
-                dir1.vm = { $set: function () {
+                dir1.ownerVM = { $set: function () {
                     removed = false
                 }}
                 dir1.unbind()
@@ -316,7 +318,7 @@ describe('UNIT: Directives', function () {
             it('should trigger vm.$set when value is changed', function () {
                 var triggered = false
                 dir.key = 'select'
-                dir.vm = { $set: function (key, val) {
+                dir.ownerVM = { $set: function (key, val) {
                     triggered = true
                     assert.strictEqual(key, 'select')
                     assert.equal(val, 1)
@@ -328,7 +330,7 @@ describe('UNIT: Directives', function () {
 
             it('should remove listener on unbind()', function () {
                 var removed = true
-                dir.vm = { $set: function () {
+                dir.ownerVM = { $set: function () {
                     removed = false
                 }}
                 dir.unbind()
@@ -360,7 +362,7 @@ describe('UNIT: Directives', function () {
             it('should trigger vm.$set when value is changed via input', function () {
                 var triggered = false
                 dir.key = 'foo'
-                dir.vm = { $set: function (key, val) {
+                dir.ownerVM = { $set: function (key, val) {
                     assert.ok(dir.lock, 'the directive should be locked if it has no filters')
                     assert.strictEqual(key, 'foo')
                     assert.strictEqual(val, 'bar')
@@ -373,8 +375,10 @@ describe('UNIT: Directives', function () {
 
             it('should remove event listener with unbind()', function () {
                 var removed = true
-                dir.vm.$set = function () {
-                    removed = false
+                dir.ownerVM = {
+                    $set: function () {
+                        removed = false
+                    }
                 }
                 dir.unbind()
                 dir.el.dispatchEvent(mockHTMLEvent('input'))
@@ -386,7 +390,7 @@ describe('UNIT: Directives', function () {
                 var dir = mockDirective('model', 'input', 'text')
                 dir.filters = []
                 dir.bind()
-                dir.vm = {$set:function () {
+                dir.ownerVM = {$set:function () {
                     assert.notOk(dir.lock)
                     triggered = true
                 }}