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

Handlebars uses "default" with the AMD option #667

Closed
arobinson opened this issue Nov 27, 2013 · 15 comments
Closed

Handlebars uses "default" with the AMD option #667

arobinson opened this issue Nov 27, 2013 · 15 comments

Comments

@arobinson
Copy link

With handlebars.js version 1.1.2 the "handlebars --amd" option now creates invalid templates.

The code has this:

output.push('define([\'' + argv.handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n  Handlebars = Handlebars["default"];');

I'm not seeing any "default" property of handlebars. The code would be fine if the assigning to Handlebars["default"] was not used.

Here is how I'm loading handlebars 1.1.2 so that it is correctly exported:

requirejs.config(
{
  "baseUrl": "/js",
  "shim":
  {
    "handlebars.runtime":
    {
      "exports": "Handlebars"
    }
  }
});

Is there something different that must be done with v1.1.2?

@arobinson
Copy link
Author

Never mind, messed up my understanding of what is going on

@arobinson arobinson reopened this Nov 27, 2013
@kpdecker
Copy link
Collaborator

This is the expected behavior as the amd module exports a default field to keep parity with the ES6 module transitional behavior. Glad to chat about specific errors if you create a fiddle demonstrating the issue though!

@arobinson
Copy link
Author

It did not work. "default" is null for me. When I delete the reassignment of the Handlebars variable it works.

It seems to me that there is an incorrect assumption that must not be true in my environment. Is there an assumed version of Require.js and perhaps mine is too old? Am I supposed to be using a different version of handlebars.runtime.js besides the one from the main download link?

I got it from:
http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars.runtime-v1.1.2.js

@kpdecker
Copy link
Collaborator

You need to use one of the amd builds if you are going to compile the templates to amd using the bundled CLI precompiler.

Ex: //cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.1.2/handlebars.runtime.amd.js
http://cdnjs.com/libraries/handlebars.js/

The bundled precompiler does not attempt to handle mixed modes where some things are defined and loaded via AMD and others are not.

@simpixelated
Copy link

I'm not sure how the AMD version is supposed to work (full, not runtime). See this fiddle:
http://jsfiddle.net/e6Z7L/1/

Why is everything attached to default and how do I fix this without calling Handlebars["default"] everywhere?

@kpdecker
Copy link
Collaborator

@wycats can you provide more info on default?

@chuwy
Copy link

chuwy commented Dec 20, 2013

Have we any other solutions beside manually add Handlebars = Handlebars.default to template file?

@kpdecker
Copy link
Collaborator

@chuwy
Copy link

chuwy commented Jan 19, 2014

No, it doesn't helped. I think, I chose wrong issue for asking help.
I tried to load handlebars.amd.js in my browser by require.js, but it returns object with default key, where HandlebarsEnvironment stored. Runtime version returns nothing.
Are handlebars supports this feature or I must use require.js shim?

@kpdecker
Copy link
Collaborator

@chuwy the default return is expected. If you're seeing a failure with the runtime version then filing another issue with a jsfiddle that reproduces the issue is the best way for us to look into the issue.

@ryan-blunden
Copy link

I realize this is closed but for clarity, I added a new JSFiddle at http://jsfiddle.net/RyanBlunden/zth1f501/ which shows how to use the Handlebars AMD Runtime with RequireJS.

@emgeebee
Copy link

emgeebee commented Dec 3, 2014

Thanks for the fiddle ryan, but how would someone do this when their templates are created via someting like grunt-contrib-handlebars?

Edit: found this #796 (comment)

@ryan-blunden
Copy link

I don't know as this is an issue with grunt-contrib-handlebars.

This is precisely why I'm not a fan of using Grunt for trivial tasks such as rendering Handlebars templates.

To see how easy it is, checkout the script at https://github.com/ryan-blunden/handlebars-requrejs/blob/master/project/bin/compile_templates_amd.sh

@tribe84
Copy link

tribe84 commented Jun 2, 2015

Having the same issues when trying to include Handlebars in karma tests.

@nasangam
Copy link

Use grunt-replace task to fix this issue:

match: /function(Handlebars) {/g,
replacement: 'function(Handlebars) { Handlebars=Handlebars.default || Handlebars;'

src: ['*/**/hbs.js'],
dest: './'

https://www.npmjs.com/package/grunt-replace

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

No branches or pull requests

8 participants