0
0
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:
Rich Harris 2016-12-08 15:19:54 -05:00 committed by GitHub
commit d23de6607c
5 changed files with 53 additions and 2 deletions

View File

@ -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;
} }

View 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>' );
}
};

View 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>

View 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>' );
}
};

View File

@ -0,0 +1,9 @@
<h1>Hello {{name()}}!</h1>
<script>
export default {
data: () => ({
name: () => 'world'
})
};
</script>