leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
648 stars 159 forks source link

Use with AMD / Require.js / requirejs-rails #54

Closed pboling closed 11 years ago

pboling commented 11 years ago

I am trying to use handlebars_assets with requirejs-rails + backbone + backbone.marionette and am having a lot of trouble.

It seems like everything is working except I have no access to my template namespace within my require closures.

This stackoverflow question does a pretty good job of going over the problem. However if I use the solution there I lose all of the benefits of handlebars_assets and the asset pipeline.

Is there a way to make handlebars_assets require.js/AMD friendly? Or is it already and I've missed something?

leshill commented 11 years ago

Hi @pboling,

I just skimmed the two links.

Fundamentally the problem is that the asset pipeline transforms a single file at a time. I skimmed the requirejs-rails README did not see anything that gives an indication that it works any differently?

As a data point, even the smallest app I have is over twenty CoffeeScript files and templates.

Perhaps you could write a template module for requirejs to use, and inside that module, use the normal asset pipeline require to load in the compiled templates?

If something like that is not possible, then the place to make handlebars_assets generate a module per template is here:

https://github.com/leshill/handlebars_assets/blob/master/lib/handlebars_assets/tilt_handlebars.rb#L40

That said, this seems very much like the wrong approach.

WDYT?

pboling commented 11 years ago

Forked and and implemented a solution that seems simple, backwards compatible, and makes sense from a RequireJS perspective. Pull request momentarily...

Apparently I had the same idea you did. Went with option 2. Option 1 would be a lot more complex.