8000 props: call default value functions with vm as the context (close #1382) · bencode/vue@e40a1ac · GitHub
[go: up one dir, main page]

Skip to content

Commit e40a1ac

Browse files
committed
props: call default value functions with vm as the context (close vuejs#1382)
1 parent f6f9f16 commit e40a1ac

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/compiler/compile-props.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function makePropsLinkFn (props) {
181181
options = prop.options
182182
if (prop.raw === null) {
183183
// initialize absent prop
184-
_.initProp(vm, prop, getDefault(options))
184+
_.initProp(vm, prop, getDefault(vm, options))
185185
} else if (prop.dynamic) {
186186
// dynamic prop
187187
if (vm._context) {
@@ -224,11 +224,12 @@ function makePropsLinkFn (props) {
224224
/**
225225
* Get the default value of a prop.
226226
*
227+
* @param {Vue} vm
227228
* @param {Object} options
228229
* @return {*}
229230
*/
230231

231-
function getDefault (options) {
232+
function getDefault (vm, options) {
232233
// no default, return undefined
233234
if (!options.hasOwnProperty('default')) {
234235
// absent boolean value defaults to false
@@ -247,6 +248,6 @@ function getDefault (options) {
247248
}
248249
// call factory function for non-Function types
249250
return typeof def === 'function' && options.type !== Function
250-
? def()
251+
? def.call(vm)
251252
: def
252253
}

test/unit/specs/directives/prop_spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ if (_.inBrowser) {
579579
prop: {
580580
type: String,
581581
default: 'hello'
582+
},
583+
prop2: {
584+
type: Object,
585+
default: function () {
586+
return { vm: this }
587+
}
582588
}
583589
},
584590
data: function () {
@@ -591,6 +597,10 @@ if (_.inBrowser) {
591597
}
592598
})
593599
expect(vm.$el.textContent).toBe('hello world')
600+
// object/array default value initializers should be
601+
// called with the correct `this` context
602+
var child = vm.$children[0]
603+
expect(child.prop2.vm).toBe(child)
594604
vm.$children[0].prop = 'bye'
595605
_.nextTick(function () {
596606
expect(vm.$el.textContent).toBe('bye world')

0 commit comments

Comments
 (0)
0