Using Backbone.js with _.noConflict()
I'm looking to use Backbone.开发者_运维问答js with a namespaced underscore library. Does anyone know how I can tell Backbone to refer to say, underscore
and not _
Thanks! Matt
As of today (version 0.5.3) Backbone isn't ready for this in it self but it can be done:
You need to put your script tags requesting underscore.js
and backbone.js
first/early among your script
tags, and do your _.noConflict()
in a script between the underscore,backbone
scripts and the rest of the script loading. Here's a schematic version:
<!DOCTYPE html>
<html>
<head>
<title>Labbo</title>
<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script>
var underscore = _.noConflict();
</script>
<script>
// In it's own script tag for readability
console.log('_ object: ', _);
console.log('"underscore" object: ', underscore);
var m = new Backbone.Model({});
console.log('Dummy backbone model: ', m);
</script>
<!-- Load your other scripts. From here on the '_' global isn't defined -->
<!-- any more. -->
<!-- <script src="your_other_scripts.js"></script> -->
</head>
<body>
Open Developer Tools / Firebug and check the output in the console.
</body>
</html>
(Couldn't put this on jsfiddle beacuse to demo you need control over excactly where the script tags go).
精彩评论