Skip to content

Commit

Permalink
Merge pull request #1652 from sveltejs/gh-1544-computed-whole-state
Browse files Browse the repository at this point in the history
exclude current prop in computed properties using entire state #1544
  • Loading branch information
Rich-Harris committed Aug 15, 2018
2 parents c8546ba + 345cf64 commit 4ce2e1d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ export default function dom(
} else {
// computed property depends on entire state object —
// these must go at the end
const arg = hasRestParam ? `@exclude(state, "${key}")` : `state`;

computationBuilder.addLine(
`if (this._differs(state.${key}, (state.${key} = %computed-${key}(${arg})))) changed.${key} = true;`
`if (this._differs(state.${key}, (state.${key} = %computed-${key}(@exclude(state, "${key}"))))) changed.${key} = true;`
);
}
});
Expand Down
25 changes: 25 additions & 0 deletions test/runtime/samples/computed-whole-state/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
html: `
<pre>{"wanted":2}</pre>
`,

test(assert, component, target) {
component.set({
unwanted: 3,
wanted: 4,
});

assert.htmlEqual(target.innerHTML, `
<pre>{"wanted":4}</pre>
`);

component.set({
unwanted: 5,
wanted: 6,
});

assert.htmlEqual(target.innerHTML, `
<pre>{"wanted":6}</pre>
`);
},
};
22 changes: 22 additions & 0 deletions test/runtime/samples/computed-whole-state/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<pre>{JSON.stringify(props)}</pre>
<script>
export default {
data: () => ({
unwanted: 1,
wanted: 2
}),

helpers: {
// prevent this being mixed in with data
JSON
},

computed: {
props: (state) => {
var result = Object.assign({}, state);
delete result.unwanted;
return result;
}
}
};
</script>

0 comments on commit 4ce2e1d

Please sign in to comment.