Making Backbone-query AMD-compatible
Rob--W opened this issue · comments
Rob Wu commented
Just a note for others, so that they don't waste valuable time on debugging this:
Backbone-query does not work out-of-the box in conjunction with the RequireJS optimizer,
In Coffeescript
- Replace (on top):
define ['underscore', 'backbone'], (_, Backbone) ->
- Replace (at the bottom):
Backbone
(to export the module variable).
In JavaScript
- Replace the top
(function(){
with `define(['underscore', 'backbone'], function(_, Backbone) { - Replace the bottom
}).call(this);
with});
(or, if an export is desired,return Backbone;});
. Don't worry about the changed context, Backbone-query doesn't usevar root = this;
or something to export variables.
If you want to create a module which is compatible with node and AMD loaders, have a look at these examples for Backbone and underscore: https://gist.github.com/2517531 :
(function(root, define) { define('underscore', function(require, exports, module) {
...
})}).call(this, this, typeof define === 'function' && define.amd ? define : function(id, factory) {
if (typeof exports !== 'undefined') {
// CommonJS has require and exports, use them and execute
// the factory function immediately. Provide a wrapper
// for require to deal with jQuery.
factory(function(id) {
// jQuery most likely cannot be loaded
// in a CommonJS environment, unless the developer
// also uses a browser shim like jsdom. Allow
// for that possibility, but do not blow
// up if it does not work. Use of a
// try/catch has precedent in Node modules
// for this kind of situation.
try {
return require(id);
} catch (e) {
// Do not bother returning a value, just absorb
// the error, the caller will receive undefined
// for the value.
}
}, exports);
} else {
// Plain browser. Grab the global.
this._ = {};
}
});