我想製作一種加載器類。試想一下:在requirejs中識別調用模塊
config.js
define(function() {
return {
appViews : [
'baseViews',
'fullDocViews',
'docCreateViews',
'accountViews',
'fullDocViews',
'notifyViews'
]
}
}
loader.js
define(['config', 'underscore'], function (Config, _) {
var Loader = {};
_.each(Config.appViews, function (view) {
var include = 'views/'+view;
Loader[view] = require([include], function (Include) {
return Include;
});
});
return Loader;
});
fullDocViews.js
define(['loader'], function (Loader) {
var moduleView = Loader.baseViews.BaseView.extend({
// Loving life here...
})
});
然而,這將創建一個循環依賴,因爲調用裝載機電話fullDocsView模塊。有沒有什麼辦法,在loader.js中,我可以從產生的視圖散列中排除調用模塊?
所以,我正在尋找的是這樣的:
define(['config', 'underscore'], function (Config, _) {
var Loader = {};
_.each(Config.appViews, function (view) {
if (view !== callingView) {
var include = 'views/'+view;
Loader[view] = require([include], function (Include) {
return Include;
});
}
});
return Loader;
});
非常感謝您的迴應。 Node有module.parent,它似乎在require.js中做我正在尋找的東西 – starsinmypockets