2
votes

While backbone-deep-model has AMD support for use with RequireJS, it depends on an underscore mixin in an external file called underscore.mixin.deepExtend.js that is not AMD compatible.

Looking at this question: How to mixin Underscore plugins in RequireJS?, it seems that I can manually mixin deepExtend into Underscore within the shim init setting in RequireJS.

...
shim: {
    ...

    'deep-model': {
        deps: 'underscore',
        init: function() {
            _.mixin(/* hash of deepExtend functions */);
            return _;
        }
    }
}
...

However, I am stuck at what to do at this point since underscore.mixin.deepExtend does not return a hash of functions that _.mixin() requires.

Is there a way to load backbone-deep-model with RequireJS without modifying source code?

2
I ended up using backbone-nested instead. It has more features and was easily shimmable. requirejs.config({ paths: { underscore: 'lib/underscore', backbone: 'lib/backbone', 'backbone-nested': 'lib/backbone-nested-v1.1.2.min', }, shim: { underscore: { exports: '_' }, backbone: { deps: ['underscore', 'jquery'], exports: 'Backbone' }, 'backbone-nested': { deps: ['backbone'], exports: 'Backbone' }, }, });unblevable

2 Answers

3
votes

This may not be the answer you're looking for, but one option that I'd personally advise is to just leave Underscore (and any mix-ins, and probably also Backbone and jQuery) out of Require entirely. The main advantage of this approach (beyond making your problem go away as a side effect) is that you don't have start every require module by importing the same library (or libraries if you do the same with Backbone/jQuery).

True, this slightly "pollutes" the global space, but in my opinion having an _ (or $ or Backbone) variable in the global namespace won't hurt anything. It will however solve your problem, avoid the need for a shim at all, and save you a lot of importing. And of course you can (and should) still use Require for your own code, as keeping your variables from "polluting" the global namespace will save you future headaches.

Just a thought.

0
votes

You can shim backbone as a dependency:

shim: {
  // this is an example, I don't know what are the actual dependencies are
  'deep-model': ['backbone', 'underscore']
}

and them when you add it as a dependency it will operate on backbone:

define(['backbone', 'deep-model'], function(Backbone) {
   // backbone now has deepModel in it
});