backbone with requirejs

2019-08-11 06:26发布

问题:

I am building a complex web app and trying to use requirejs with backbone. I found out that backbonejs is a non AMD framework which restricts me to use requirejs out of the box.

I researched on internet and found that there are patches available to make backbone AMD compliant.

Please guide if patching this way is a no-issue setup. Will i run into any issues during the app development due to patching of core file in backbonejs?

I apologise if its a stupid question :)

回答1:

Nope, it's not a stupid question. We actually have done this and is working fine for us, so far :)

You can follow this guide on how to do it: http://kilon.org/blog/2012/08/build-backbone-apps-using-requirejs/

Don't forget to read the chapter on unit testing it with Jasmine. Pretty nice combination all together.



回答2:

Before Require.js 2.0, you have to patch Backbone to be AMD compliant. You can find some AMD-compliant forks of Backbone on github(e.g. amdjs). Fortunately, Require.js 2.0+ added support for loading non-AMD compatible scripts by using the Shim configuration. Example:

requirejs.config({
    shim: {
      "backbone": {
          deps: ["underscore", "jquery"],
          exports: "Backbone"
      }
    },

    paths: {
    // as usual
});