javascript - Backbone.js 是否需要 RequireJS "shim"?

标签 javascript backbone.js requirejs amd

最新版本的 Backbone.js 和 Underscore.js 似乎支持 AMD。

那么我假设不再需要在 require.js 配置中“填充”这些库是否正确?

最佳答案

是的,你是对的,不需要垫片。而且很容易测试,这是最简单的设置:

requirejs.config({
    /**
     * Paths to lib dependencies.
     *
     * Use non-minified files where possible as they will be minified (and
     * optimized via uglify) on release build (r.js)
     */
    paths: {
        "jquery": "libs/jquery/dist/jquery",
        "underscore": "libs/underscore/underscore",
        "backbone": "libs/backbone/backbone",
    },

    deps: ["app"] // starts the app
});

并确保它有效并且使用的不是全局下划线:

// I'm using Underscore as to avoid conflicting with the global _
// but you could use _ as the name for the local variable as well.
define(['backbone', 'underscore'], function(Backbone, Underscore) {
    console.log("Backbone:", Backbone.VERSION)
    console.log("Local Underscore:", Underscore.VERSION);
    console.log("Global Underscore:", _.VERSION, _ === Underscore);
});

对于Backbone,在the source中很清楚默认情况下支持 AMD:

// 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, _, $);
  });

下划线是 registering itself at the end :

// AMD registration happens at the end for compatibility with AMD loaders
// that may not enforce next-turn semantics on modules. Even though general
// practice for AMD registration is to be anonymous, underscore registers
// as a named module because, like jQuery, it is a base library that is
// popular enough to be bundled in a third party lib, but not be part of
// an AMD load request. Those cases could generate an error when an
// anonymous define() is called outside of a loader request.
if (typeof define === 'function' && define.amd) {
  define('underscore', [], function() {
    return _;
  });
}

jQuery 相同:

// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.

// Note that for maximum portability, libraries that are not jQuery should
// declare themselves as anonymous modules, and avoid setting a global if an
// AMD loader is present. jQuery is a special case. For more information, see
// https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon

if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    } );
}

关于javascript - Backbone.js 是否需要 RequireJS "shim"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603241/

相关文章:

api - 如何从集合中获取api返回的数据?

c# - 无论屏幕尺寸或分辨率如何,定位加载指示器 div

javascript - 如何检测用户是否点击了 "back"按钮

javascript - backbone.js 和带有字母的 id 出现问题

javascript - 了解带有 grunt 和主干以及相对路径的 r.js 选项

javascript - 使用 RequireJS 2.1 的 AMD 样式模块中的 TypeScript 1.8 相对模块名称解析

html - Aurelia-Cli 添加需要 .scss 文件

javascript - 如果邮政编码以 ?? 开头,则需要支付运费Javascript 或 PHP

javascript - D3 : Combine two colour scales into one

javascript - Backbone.History.fragment 在路由器事件回调中未定义