2017-12-02 3 views

Antwort

1

mit Ja, Sie sind richtig, kein Shim erforderlich. Und es ist einfach zu testen, hier ist es am einfachsten:

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 
}); 

und sicherzustellen, dass es funktioniert und es ist nicht der globale Unders, die verwendet wird:

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

Für Backbone, es ist klar, in the source, dass es unterstützt AMD standardmäßig:

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

Was Unders ist es 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 _; 
    }); 
} 

Die gleiche Sache mit 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; 
    }); 
} 
0

Wie @ggozad erwähnt:

Nun, wenn Strich bereits und verfügbar geladen ist, brauchen Sie nicht die Shim überhaupt. Backbone lädt glücklich. Wenn nicht, ist es wahrscheinlich weil Unterstrich ist nicht tatsächlich geladen.

Es klingt aber falsch, nur teilweise mit require.js, Sie könnte auch AMD-laden sie alle.

Ich denke, das erklärt, warum Sie es nicht laden müssen require.js

+0

Ich bin nicht sicher, dass Sie richtig die Frage verstanden. Die Dokumentation besagt "Erinnern Sie sich: Verwenden Sie nur shim config für Nicht-AMD-Skripte, Skripte, die nicht bereits define() aufrufen. Die Shim-Konfiguration wird nicht korrekt funktionieren, wenn sie auf AMD-Skripten verwendet wird, insbesondere werden die Exporte und init config nicht sein ausgelöst, und die Deps-Konfiguration wird in diesen Fällen verwirrend sein. " Wie ich es verstehe, haben Backbone und Underscore beide Unterstützung für AMD, wenn das der Fall ist, dann wäre das Hinzufügen eines Shims zur Konfiguration für Backbone nicht länger notwendig, oder? – master00

Verwandte Themen