javascriptjquerybackbone.jsrequirejszepto

RequireJS + Backbone without jQuery?


I'm trying to use RequireJS, here is my config file :

require.config({
  baseUrl: "/scripts",
  paths: {
    zepto: "zepto.min",
    underscore: "underscore.min",
    backbone: "backbone.min"
  },
  shim: {
    zepto: {
      exports: "$"
    },
    underscore: {
      exports: "_"
    },
    backbone: {
      deps: ["underscore", "zepto"],
      exports: "Backbone"
    }
  }
});

And this is my app.js :

require(['backbone'], function(Backbone) {
  console.log('loaded!');
});

This works fine, but I don't know why RequireJS is trying to load jQuery.


Solution

  • Because Backbone requires module called jquery (look at top of backbone.js) file.

      // Set up Backbone appropriately for the environment. Start with AMD.
      if (typeof define === 'function' && define.amd) {
        define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
          // Export global even in AMD case in case this script is loaded with
          // others that may still expect a global Backbone.
          root.Backbone = factory(root, exports, _, $);
        });
    

    and You haven't defined this module.

    To hack this use zepto as jquery:

    require.config({
      baseUrl: "/scripts",
      paths: {
        jquery: "zepto.min",
        underscore: "underscore.min",
        backbone: "backbone.min"
      },
      shim: {
        jquery: {
          exports: "$"
        },
        underscore: {
          exports: "_"
        }
      }
    });
    

    And second: shim only works with non-amd modules. Backbone is AMD module.