Using require.js with Twitter Boostrap API and bac

2019-07-17 04:06发布

问题:

Regarding this entry Loading Backbone and Underscore using RequireJS it is quite clear to me how do I configure Backbone-specific scripts and JQuery.

But how do I:

  • configure Twitter bootstrap.js?
  • what about json2.js?

Thanks!

回答1:

In addition to what you learned about the path config option, you should also review the shim config option http://requirejs.org/docs/api.html#config-shim.

Many plugins are not AMD ready so you have two options. Either configure it as a shim (suitable for most plugins), or write your own adapters like the efforts at https://github.com/amdjs

Simple example:

require.config({
    shim: {
        'bootstrap': ['jquery'], // no exports
        'underscore': { exports: '_' }, // no dependencies
        'backbone.layoutmanager': {
            deps: ['backbone']
            exports: 'Backbone.LayoutManager'
        } // a mix of exports and dependencies
     }
});

For something like json2 which has no dependencies and only activates if the browser has no native implementation, you can simply list it as a dependency of your main application's require without a wrapper / shim.