Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Ember Handlebars precompiler throws an error when using variables beginning with '@' #3030

Closed
wants to merge 1 commit into from

Conversation

stephendeyoung
Copy link

In Handlebars templates you should be able to use a variable beginning with @. Handlebars itself provides the @index variable inside an #each block. However, using any variable beginning with @ such as @index or any other variable you might define using a Handlebars helper will throw an error:

SyntaxError: Unexpected token ,
at Object.Function (<anonymous>)
at Object.JavaScriptCompiler.createFunctionContext (http://localhost:9292/handlebars.js:1500:23)
at Object.JavaScriptCompiler.compile (http://localhost:9292/handlebars.js:1415:17)
at Object.Ember.Handlebars.precompile (ember-handlebars-compiler:247:52)
at Object.templateString (ember-handlebars/~tests/handlebars_test:1812:41)
at Object.Test.run (http://localhost:9292/qunit/qunit.js:190:18)
at http://localhost:9292/qunit/qunit.js:348:10
at process (http://localhost:9292/qunit/qunit.js:1420:24)
at http://localhost:9292/qunit/qunit.js:466:5

Using the ordinary Handlebars precompiler does not throw an error when you try to use an @ variable.

You can see the @index error here and the custom variable error here. This pull request includes two failing unit tests.

@wycats
Copy link
Member

wycats commented Jul 27, 2013

This is really a request to support helper-supplied data variables in Ember, which is on the docket.

@itsjavi
Copy link

itsjavi commented Sep 1, 2013

@wycats I didn't found the feature request post talking about this, but I think, in my humble opinion, array indexes are a very essential part of programming and therefore this issue should be considered a bug and not a feature request ;)
I tried to see the ember.js source to figure out which lines causes this bug, but I think that is more complicated than a few lines, so I trust in the ember team to fix this for the 1.0 release, if possible ;) (i can see that it is planned for that release but the issue is closed...)

@itsjavi
Copy link

itsjavi commented Sep 1, 2013

I've found a temporary solution by using {{_view.contentIndex}}
http://jsfiddle.net/SRKD5/2/

@chrisvariety
Copy link
Contributor

@xaguilars you can always calculate this index yourself, e.g.

{{#each object in collection}}
  Index: {{index object collection}}
{{/each}}
Ember.Handlebars.registerBoundHelper('index', function(objectInCollection, collection) {
  return collection.indexOf(objectInCollection);
});

@itsjavi
Copy link

itsjavi commented Sep 1, 2013

@speedmanly nice example, but won't this impact in the performance of the app as it have to seek for the index each time in the loop?

@chrisvariety
Copy link
Contributor

Definitely, but I'm not sure how much it would really matter unless your
arrays are super large. Profile it & find out ! :)

On Saturday, August 31, 2013, Javier Aguilar wrote:

@speedmanly https://github.com/speedmanly nice example, but won't this
impact in the performance of the app as it have to seek for the index each
time in the loop?


Reply to this email directly or view it on GitHubhttps://github.com//pull/3030#issuecomment-23618232
.

@dfreeman
Copy link
Contributor

dfreeman commented Jun 9, 2014

@wycats Is support for helper-exposed data still on the docket for a future Ember release? Attempting to reference such variables in a template still appears to cause a syntax error when compiling the resulting function string.

@blessanm86
Copy link

Is there any update for the helper-supplied data variables feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants