Skip to content

Commit

Permalink
allow functions in data/computed - fixes #163
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 8, 2016
1 parent 5722e3a commit 81c2dc2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export default function generate ( parsed, source, options, names ) {
if ( isReference( node, parent ) ) {
const { name } = flattenReference( node );

if ( parent && parent.type === 'CallExpression' && node === parent.callee ) {
if ( generator.helpers[ name ] ) generator.code.prependRight( node.start, `template.helpers.` );
if ( parent && parent.type === 'CallExpression' && node === parent.callee && generator.helpers[ name ] ) {
generator.code.prependRight( node.start, `template.helpers.` );
return;
}

Expand Down
14 changes: 14 additions & 0 deletions test/generator/computed-function/_config.js
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions test/generator/computed-function/main.html
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions test/generator/default-data-function/_config.js
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions test/generator/default-data-function/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Hello {{name()}}!</h1>

<script>
export default {
data: () => ({
name: () => 'world'
})
};
</script>

0 comments on commit 81c2dc2

Please sign in to comment.