Skip to content

Commit 1390fa7

Browse files
committed
Fixed function prop default issue
1 parent 32c09de commit 1390fa7

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ class MyComponent {
8686
})
8787
someDefaultProp:string = 'some default value';
8888

89-
@Prop someObjProp:{default:string} = {default: 'value'}; //vue-typescript makes sure to deep clone default values for array and object types
89+
@Prop someObjProp:{some_default:string} = {some_default: 'value'}; //vue-typescript makes sure to deep clone default values for array and object types
90+
91+
@Prop someFuncProp(){ //defined functions decorated with prop are treated as the default value
92+
console.log('logged from default function!');
93+
}
9094

9195
someVar:string = 'Hello!';
9296

@@ -111,6 +115,12 @@ Vue.component('my-component', {
111115
default: 'value'
112116
}
113117
}
118+
},
119+
someFuncProp: {
120+
type: Function //if it finds the default is a function, it automatically sets the type
121+
default: function(){
122+
console.log('logged from default function!');
123+
}
114124
}
115125
},
116126
data: function(){

lib/vuecomponent.js

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/vuecomponent.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vuecomponent.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,20 @@ function createDecorator(name?:string, options?:vuejs.ComponentOption){
9292
}
9393

9494
for (key in options.props) {
95-
var default_val:any = options.data[key];
96-
if (default_val != null && default_val != undefined) {
97-
if (!options.props[key]) options.props[key] = {};
98-
if (typeof default_val == 'object'){
95+
var default_val = options.data[key];
96+
if (default_val == null || default_val == undefined) default_val = options.methods[key]
97+
if (default_val != null && default_val != undefined) {
98+
if (!options.props[key])
99+
options.props[key] = {};
100+
if(typeof default_val == 'function') options.props[key].type = Function;
101+
if (typeof default_val == 'object') {
99102
var copy = clone(default_val, false);
100-
default_val = function(){return clone(copy, false)}
103+
default_val = function () { return clone(copy, false); };
101104
}
102105
options.props[key].default = default_val;
103-
delete options.data[key];
104106
}
107+
delete options.data[key];
108+
delete options.methods[key];
105109
}
106110

107111
for( var i in newi.$$methodsToRemove){

test/prop.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ describe('Prop', function(){
2929
option_prop:string
3030
}
3131

32+
@VueComponent
33+
class FunctionPropTest {
34+
@Prop defaultFunc(){
35+
return 10;
36+
}
37+
}
38+
3239
it('should have a simple prop', function(){
3340
var component = new PropTest();
3441
expect(component.$options['props']).to.have.property('simple_prop').that.has.property('default').that.equals('default val');
@@ -56,4 +63,11 @@ describe('Prop', function(){
5663
expect(o1()).to.not.equal(o2());
5764
})
5865

66+
it('should remove functrions from methods', function(){
67+
var component = Utils.component('function-prop-test');
68+
expect(component.$options.props).to.have.property('defaultFunc');
69+
expect(component.$options.props.defaultFunc.default()).to.equal(10);
70+
expect(component.$options.methods).not.to.have.property('defaultFunc');
71+
})
72+
5973
})

0 commit comments

Comments
 (0)