Skip to content

Commit

Permalink
Merge pull request #276 from sveltejs/gh-274
Browse files Browse the repository at this point in the history
handle default parameters in computed values
  • Loading branch information
Rich-Harris authored Feb 1, 2017
2 parents 17e31df + 11e613e commit face13a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export default class Generator {
const key = prop.key.name;
const value = prop.value;

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

Expand Down
8 changes: 4 additions & 4 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ export default function dom ( parsed, source, options, names ) {

computations.forEach( ({ key, deps }) => {
builder.addBlock( deindent`
if ( ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) {
if ( isInitial || ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) {
state.${key} = newState.${key} = template.computed.${key}( ${deps.map( dep => `state.${dep}` ).join( ', ' )} );
}
` );
});

builders.main.addBlock( deindent`
function applyComputations ( state, newState, oldState ) {
function applyComputations ( state, newState, oldState, isInitial ) {
${builder}
}
` );

builders._set.addLine( `applyComputations( this._state, newState, oldState )` );
builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` );
}

// TODO is the `if` necessary?
Expand Down Expand Up @@ -277,7 +277,7 @@ export default function dom ( parsed, source, options, names ) {
function ${name} ( options ) {
options = options || {};
${generator.usesRefs ? `\nthis.refs = {}` : ``}
this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {} );` : ``}
this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {}, true );` : ``}
this._observers = {
pre: Object.create( null ),
Expand Down
9 changes: 9 additions & 0 deletions test/generator/computed-values-default/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
html: '<p>2</p>',

test ( assert, component, target ) {
component.set({ a: 2 });
assert.equal( target.innerHTML, '<p>4</p>' );
component.teardown();
}
};
9 changes: 9 additions & 0 deletions test/generator/computed-values-default/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>{{foo}}</p>

<script>
export default {
computed: {
foo: ( a = 1 ) => a * 2
}
};
</script>
1 change: 1 addition & 0 deletions test/generator/computed-values/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default {
assert.equal( component.get( 'c' ), 5 );
assert.equal( component.get( 'cSquared' ), 25 );
assert.equal( target.innerHTML, '<p>3 + 2 = 5</p>\n<p>5 * 5 = 25</p>' );
component.teardown();
}
};
File renamed without changes.
File renamed without changes.

0 comments on commit face13a

Please sign in to comment.