我有两个小的 Backbone 插件,如下所示:
(function($, _, Backbone) {
var ListView = Backbone.View.extend({
// blablabla
});
Backbone.ListView = ListView;
})($, _, Backbone);
和
(function($, _, Backbone) {
var Repository = Backbone.Model.extend({
// blablabla
});
Backbone.Repository = Repository;
})($, _, Backbone);
我现在已经设置了 require.config:
require.config({
baseUrl: "javascripts",
shim: {
"jquery": {
exports: "$"
},
"underscore": {
exports: "_"
},
"backbone": {
deps: ['jquery', 'underscore'],
exports: "Backbone"
},
"ListView": {
deps: ['jquery', 'underscore', 'backbone'],
exports: "Backbone.ListView"
},
"Repository": {
deps: ['jquery', 'underscore', 'backbone'],
exports: "Backbone.Repository"
}
},
paths: {
"jquery": "Vendors/jquery",
"underscore": "Vendors/underscore",
"backbone": "Vendors/backbone",
"ListView": "Extensions/ListView",
"Repository": "Extensions/Repository"
}
});
现在我们来解决问题。如果我想同时使用这两个插件,这就是我目前必须处理模块依赖项的方式:
require(['jquery', 'underscore', 'ListView', 'Repository'], function($, _, Backbone1, Backbone2) {
// this is backbone + list view
console.log(Backbone1);
// this is backbone + repository
console.log(Backbone2);
});
但我想让插件已经注册到主干:
require(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
// this now is backbone + listView + repository
console.log(Backbone);
});
我该怎么做呢?我必须改变什么?
最好的问候,博多