与 require.js 作为 AMD 模块一起使用时覆盖 underscore.js 默认值
由于下划线是作为 AMD 模块加载的,因此任何人都可以建议如何最好地针对下划线设置全局选项,以便我仍然可以在相同的统一模块名称下访问它并让我的覆盖可用。
我已经尝试过了,到目前为止,我基本上有一个包装器,其中包含下划线作为依赖项,进行修改,然后返回修改后的下划线。
paths: {
'underscore': 'libs/underscore/1.2.3/underscore',
'underscore-override': 'libs/underscore/define'
}
在define.js 中,我基本上有以下内容:
define(['underscore'], function (_) {
// use mustache syntax
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
return _;
});
这种方法有效,但我确信有一种更优雅的方法来实现此目的,这样我就不必使用“underscore-custom”。
有什么建议吗?
干杯:-)
更新:根据已接受答案中的建议,我现在执行以下操作:
libs/bootstrap.js
define([
// add custom library defines here
'../libs/underscore/define'
], function () {
var libs = [].slice.call(arguments),
initialize = function () {
libs.forEach(function (lib) {
if (typeof lib === 'function') {
lib();
}
});
}
return {
initialize : initialize
}
});
libs/underscore/define
define(['underscore'], function(_) {
return function () {
// add custom settings here
};
});
main.js
require(['libs'], function(libs){
libs.initialize();
});
Since underscore is loaded in as an AMD module does anyone have an recommendations on how best to set global options against underscore so that I can still access it under the same unified module name and have my overrides available.
I've played around with it and so far I basically have a wrapper in which I include underscore as a dependancy, make my modifications and then return the modified underscore.
paths: {
'underscore': 'libs/underscore/1.2.3/underscore',
'underscore-override': 'libs/underscore/define'
}
Inside define.js I basically have the following:
define(['underscore'], function (_) {
// use mustache syntax
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
return _;
});
This approach works but I'm sure there is a much more elegant way to achieve this so that I don't have to use 'underscore-custom'.
Any suggestions?
Cheers :-)
UPDATE: Based on the suggestions in the accepted answer I now do the following:
libs/bootstrap.js
define([
// add custom library defines here
'../libs/underscore/define'
], function () {
var libs = [].slice.call(arguments),
initialize = function () {
libs.forEach(function (lib) {
if (typeof lib === 'function') {
lib();
}
});
}
return {
initialize : initialize
}
});
libs/underscore/define
define(['underscore'], function(_) {
return function () {
// add custom settings here
};
});
main.js
require(['libs'], function(libs){
libs.initialize();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能有一个配置器模块,可以加载任何需要配置的内容。
You could possibly have a configurator module that loads in anything that needs to be configured.