Browse files

fix template switch statement

  • Loading branch information...
1 parent c2b38dc commit 306442567a474bd346f64c49606ad63e45ddc3f2 @marcuswestin committed May 2, 2012
Showing with 24 additions and 3 deletions.
  1. +3 −3 src/compiler.js
  2. +21 −0 test/tests/test-4-compiler.js
View
6 src/compiler.js
@@ -400,20 +400,20 @@ var _compileIfStatement = function(blockCompileFn, context, ast) {
var _compileSwitchStatement = function(blockCompileFn, context, ast) {
return code(
';(function(branches) {',
- ' switch ({{ expression }}) {',
+ ' switch ({{ expression }}.getContent()) {',
map(ast.cases, function(switchCase, i) {
var labels = switchCase.isDefault
? 'default:\n'
: map(switchCase.values, function(value) {
- return 'case ' + compileExpression(context, value) + ':\n'
+ return 'case ' + compileExpression(context, value) + '.getContent():\n'
}).join('')
return labels
+ 'branches['+i+'](); break'
}).join('\n'),
' }',
'})([',
map(ast.cases, function(switchCase, i) {
- return 'function branches'+i+'(){ ' + indent(blockCompileFn, switchContext, switchCase.statements) + '}'
+ return 'function branches'+i+'(){ ' + indent(blockCompileFn, context, switchCase.statements) + '}'
}).join(',\n'),
'])',
{
View
21 test/tests/test-4-compiler.js
@@ -279,6 +279,27 @@ test("For loop only executes once for a push").code(
.click('#output')
.textIs('#output', '5')
+test('switch').code(
+ 'foo = "foo"',
+ '<div id="output">switch foo {',
+ ' case "foo": "foo"',
+ ' case "cat": "cat"',
+ ' default: foo',
+ '}</div onclick=handler() {',
+ ' switch foo {',
+ ' case "foo": foo set:"cat"',
+ ' case "cat": foo set:"tag"',
+ ' default: foo set:"wee"',
+ '}}>')
+ .textIs('#output', "foo")
+ .click('#output')
+ .textIs('#output', "cat")
+ .click('#output')
+ .textIs('#output', "tag")
+ .click('#output')
+ .textIs('#output', "wee")
+
+
test("copy of grouped expression (foo + 1).copy()").code(
'foo = 1',
'<div id="output">foo</div onclick=handler() { foo set: (foo + 2).copy() }>')

0 comments on commit 3064425

Please sign in to comment.