davidgtonge / backbone_query

A lightweight query api for Backbone Collections
MIT License
378 stars 29 forks source link

Making Backbone-query AMD-compatible #8

Closed Rob--W closed 11 years ago

Rob--W commented 12 years ago

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

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._ = {};
  }
});