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

make `this` context the vm instance in data and el option functions

Evan You 11 лет назад
Родитель
Сommit
9649990757
2 измененных файлов с 13 добавлено и 8 удалено
  1. 3 3
      src/util/merge-option.js
  2. 10 5
      test/unit/specs/util/merge-option_spec.js

+ 3 - 3
src/util/merge-option.js

@@ -34,10 +34,10 @@ strats.data = function (parentVal, childVal, vm) {
     return childVal || parentVal
   }
   var instanceData = typeof childVal === 'function'
-    ? childVal()
+    ? childVal.call(vm)
     : childVal
   var defaultData = typeof parentVal === 'function'
-    ? parentVal()
+    ? parentVal.call(vm)
     : undefined
   if (instanceData) {
     // mix default data into instance data
@@ -68,7 +68,7 @@ strats.el = function (parentVal, childVal, vm) {
   var ret = childVal || parentVal
   // invoke the element factory if this is instance merge
   return vm && typeof ret === 'function'
-    ? ret()
+    ? ret.call(vm)
     : ret
 }
 

+ 10 - 5
test/unit/specs/util/merge-option_spec.js

@@ -160,23 +160,26 @@ describe('Util - Option merging', function () {
   })
 
   it('instanace el merge', function () {
+    var vm = {} // mock vm presence
     function fn1 () {
+      expect(this).toBe(vm)
       return 1
     }
     function fn2 () {
+      expect(this).toBe(vm)
       return 2
     }
     // both functions
-    var res = merge({el:fn1}, {el:fn2}, {})
+    var res = merge({el:fn1}, {el:fn2}, vm)
     expect(res.el).toBe(2)
     // direct instance el
-    res = merge({el:fn1}, {el:2}, {})
+    res = merge({el:fn1}, {el:2}, vm)
     expect(res.el).toBe(2)
     // no parent
-    res = merge({}, {el:2}, {})
+    res = merge({}, {el:2}, vm)
     expect(res.el).toBe(2)
     // no child
-    res = merge({el:fn1}, {}, {})
+    res = merge({el:fn1}, {}, vm)
     expect(res.el).toBe(1)
   })
 
@@ -192,16 +195,18 @@ describe('Util - Option merging', function () {
   })
 
   it('instance data merge with default data function', function () {
+    var vm = {} // mock vm presence
     var res = merge(
       // component default
       { data: function () {
+        expect(this).toBe(vm)
         return {
           a: 1,
           b: 2
         }
       }},
       { data: { a: 2 }}, // instance data
-      {} // mock vm presence
+      vm
     )
     expect(res.data.a).toBe(2)
     expect(res.data.b).toBe(2)