Merge pull request #164 from sveltejs/gh-163

allow functions in data/computed
pull/173/head
Rich Harris 8 years ago committed by GitHub
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;
} }

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

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

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

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