Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js helpers and Handlebars.js (runtime-only build)

Is there any way to precompile Handlebars template, so that it will be aware about Ember.js helpers, like "action", "view", etc.?

The reason I'm asking is because I need to use Ember.js with a runtime-only build of the Handlebars (due to CSP in Google Chrome), and it seems that Ember.js does not completely support currently such scenario: https://github.com/emberjs/ember.js/issues/1303

like image 946
s.ermakovich Avatar asked Oct 07 '22 21:10

s.ermakovich


1 Answers

You can precompile an Ember Handlebars template using Ember.Handlebars.precompile (ember-handlebars/lib/ext.js#L104).

The basic strategy to use for precompilation outside of a browser (in node, for example) is:

  • Load handlebars.js
  • Load ember.js
  • Shim out a few browser APIs that Ember expects (this should not be necessary in 1.0, but it is necessary today).
  • template = Ember.Handlebars.precompile(string)
  • Ember.TEMPLATES[name] = Ember.Handlebars.template(template)

The ember-rails gem for Rails does this for you if you have an asset with the extension .handlebars.

The ember-rails gem also maintains a shim .js file that you can use to precompile in other non-browser environments (ember-precompiler.js).

like image 136
Yehuda Katz Avatar answered Oct 10 '22 01:10

Yehuda Katz