Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle default parameters in computed values #276

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
};