mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
Merge pull request #164 from sveltejs/gh-163
allow functions in data/computed
This commit is contained in:
commit
d23de6607c
@ -137,8 +137,8 @@ export default function generate ( parsed, source, options, names ) {
|
|||||||
if ( isReference( node, parent ) ) {
|
if ( isReference( node, parent ) ) {
|
||||||
const { name } = flattenReference( node );
|
const { name } = flattenReference( node );
|
||||||
|
|
||||||
if ( parent && parent.type === 'CallExpression' && node === parent.callee ) {
|
if ( parent && parent.type === 'CallExpression' && node === parent.callee && generator.helpers[ name ] ) {
|
||||||
if ( generator.helpers[ name ] ) generator.code.prependRight( node.start, `template.helpers.` );
|
generator.code.prependRight( node.start, `template.helpers.` );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
test/generator/computed-function/_config.js
Normal file
14
test/generator/computed-function/_config.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
export default {
|
||||||
|
html: '<p>50</p>',
|
||||||
|
|
||||||
|
test ( assert, component, target ) {
|
||||||
|
component.set({ range: [ 50, 100 ] });
|
||||||
|
assert.htmlEqual( target.innerHTML, '<p>75</p>' );
|
||||||
|
|
||||||
|
component.set({ range: [ 50, 60 ] });
|
||||||
|
assert.htmlEqual( target.innerHTML, '<p>55</p>' );
|
||||||
|
|
||||||
|
component.set({ x: 8 });
|
||||||
|
assert.htmlEqual( target.innerHTML, '<p>58</p>' );
|
||||||
|
}
|
||||||
|
};
|
20
test/generator/computed-function/main.html
Normal file
20
test/generator/computed-function/main.html
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<p>{{scale(x)}}</p>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data: () => ({
|
||||||
|
x: 5,
|
||||||
|
domain: [ 0, 10 ],
|
||||||
|
range: [ 0, 100 ]
|
||||||
|
}),
|
||||||
|
|
||||||
|
computed: {
|
||||||
|
scale: ( domain, range ) => {
|
||||||
|
return num => {
|
||||||
|
const t = domain[0] + ( num - domain[0] ) / ( domain[1] - domain[0] );
|
||||||
|
return range[0] + t * ( range[1] - range[0] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
8
test/generator/default-data-function/_config.js
Normal file
8
test/generator/default-data-function/_config.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default {
|
||||||
|
html: '<h1>Hello world!</h1>',
|
||||||
|
|
||||||
|
test ( assert, component, target ) {
|
||||||
|
component.set({ name: () => 'everybody' });
|
||||||
|
assert.htmlEqual( target.innerHTML, '<h1>Hello everybody!</h1>' );
|
||||||
|
}
|
||||||
|
};
|
9
test/generator/default-data-function/main.html
Normal file
9
test/generator/default-data-function/main.html
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<h1>Hello {{name()}}!</h1>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data: () => ({
|
||||||
|
name: () => 'world'
|
||||||
|
})
|
||||||
|
};
|
||||||
|
</script>
|
Loading…
Reference in New Issue
Block a user