I would suggest first of all to create modules for ListView
and Repository
to keep them (and hopefully Backbone
as well) out of the global namespace, like this:
list_view.js
define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
Backbone.ListView = Backbone.View.extend({
... ListView code here ...
}
return Backbone.ListView
});
repository.js
define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
Backbone.Repository = Backbone.View.extend({
... Repository code here ...
}
return Backbone.Repository
});
Then you can use require.js' map
config to map dependencies to an adapter where you include both modules, and then undo the mapping for the list_view
and repository
modules themselves to avoid circular dependencies:
requirejs.config({
map: {
'*': {
'backbone': 'backbone-adapter'
},
'list_view': {
'backbone': 'backbone'
},
'repository': {
'backbone': 'backbone'
},
});
Then create a file backbone-adapter.js
to bundle the plugins with Backbone itself:
backbone-adapter.js
define(['backbone', 'list_view', 'repository'], function (Backbone) {
return Backbone;
});
Then in your modules, when you include 'backbone'
as a dependency, requirejs will map that to backbone-adapter
, which will in turn include your plugins so that they are available as Backbone.ListView
and Backbone.Repository
.
I haven't actually tested the code above but I use a similar strategy for bundling vendor modules with my own plugins and it's worked fine for me. (The idea is taken from this discussion.)