Skip to content

Commit face13a

Browse files
authored
Merge pull request #276 from sveltejs/gh-274
handle default parameters in computed values
2 parents 17e31df + 11e613e commit face13a

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

src/generators/Generator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export default class Generator {
246246
const key = prop.key.name;
247247
const value = prop.value;
248248

249-
const deps = value.params.map( param => param.name );
249+
const deps = value.params.map( param => param.type === 'AssignmentPattern' ? param.left.name : param.name );
250250
dependencies.set( key, deps );
251251
});
252252

src/generators/dom/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,19 @@ export default function dom ( parsed, source, options, names ) {
185185

186186
computations.forEach( ({ key, deps }) => {
187187
builder.addBlock( deindent`
188-
if ( ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) {
188+
if ( isInitial || ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) {
189189
state.${key} = newState.${key} = template.computed.${key}( ${deps.map( dep => `state.${dep}` ).join( ', ' )} );
190190
}
191191
` );
192192
});
193193

194194
builders.main.addBlock( deindent`
195-
function applyComputations ( state, newState, oldState ) {
195+
function applyComputations ( state, newState, oldState, isInitial ) {
196196
${builder}
197197
}
198198
` );
199199

200-
builders._set.addLine( `applyComputations( this._state, newState, oldState )` );
200+
builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` );
201201
}
202202

203203
// TODO is the `if` necessary?
@@ -277,7 +277,7 @@ export default function dom ( parsed, source, options, names ) {
277277
function ${name} ( options ) {
278278
options = options || {};
279279
${generator.usesRefs ? `\nthis.refs = {}` : ``}
280-
this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {} );` : ``}
280+
this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {}, true );` : ``}
281281
282282
this._observers = {
283283
pre: Object.create( null ),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
html: '<p>2</p>',
3+
4+
test ( assert, component, target ) {
5+
component.set({ a: 2 });
6+
assert.equal( target.innerHTML, '<p>4</p>' );
7+
component.teardown();
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>{{foo}}</p>
2+
3+
<script>
4+
export default {
5+
computed: {
6+
foo: ( a = 1 ) => a * 2
7+
}
8+
};
9+
</script>

test/generator/computed-values/_config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default {
55
assert.equal( component.get( 'c' ), 5 );
66
assert.equal( component.get( 'cSquared' ), 25 );
77
assert.equal( target.innerHTML, '<p>3 + 2 = 5</p>\n<p>5 * 5 = 25</p>' );
8+
component.teardown();
89
}
910
};

0 commit comments

Comments
 (0)