0

I am trying to make a module that uses jQuery and Handlebars

This is the main file:

require(['app', 'jquery', 'handlebars' ], function (app, $, handlebars) {

    console.log('Running jQuery %s', $().jquery);
});

And this is the app file:

define(['jquery', 'handlebars'], function ($, hb) {

    $(function() {

        var source   = $("#some-template").html();
        var template = hb.compile(source);
        var data = {...});
});

Why is does it say hb is not defined but when I remove all dependencies it works when using Handlebars instead of hb (which is the normal way)?

4
  • Note: it's unnecessary (and generally confusing) to include dependencies for modules which are not used and are only needed by sub-modules; in your case main doesn't need handlebars (and jQuery is there only for debugging, right?) so it should not be listed in the define call. Commented Oct 27, 2013 at 14:14
  • @kryger What do you mean not used? bot jquery and handlebars are used inside the module, unless I confuse the terms here? Commented Oct 27, 2013 at 18:00
  • @kryger I am new to require.js, so what would be the right way to do it? Commented Oct 27, 2013 at 18:04
  • The signature of your "main" file should be require(['app', 'jquery' ], function (app, $) (...) if you're not using Handlebars in that particular module. Commented Oct 31, 2013 at 10:54

1 Answer 1

2

Handlebars is not AMD/RequireJS compliant. You will need to shim it: http://requirejs.org/docs/api.html#config-shim

  shim: {
    handlebars: {
      exports: 'Handlebars'
    },
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.