由于我没有可使用的代码示例,因此我将为您提供一些通用的解决方案。
- 在数组中收集设置对象并在覆盖设置区域内对其进行迭代。
- 通过窗口公开您的插件并直接在此处附加设置。
JS
//File 1.js
window.settings = window.settings || [];
window.settings.push({a:'11'});
//File2.js
window.settings = window.settings || [];
window.settings.push({b:'22', c:'33'});
//File3.js
window.settings = window.settings || [];
window.settings.push({b:'222'});
//Main.js File
;(function ($,window,undefined){
var plugin = function(settings){
var defConfig = {
a: '1',
b: '2',
c: '3'
};
var len = settings.length, i;
if(len){
for(i = 0; i < len; i++){
defConfig = $.extend(defConfig, settings[i]);
}
}else{
defConfig = $.extend(defConfig, settings);
}
alert(JSON.stringify(defConfig));
};
var instance = new plugin(window.settings || []);
})(jQuery,window);
//Main.js File
;(function ($,window,undefined){
var plugin = function(){
var defConfig = {
a: '1',
b: '2',
c: '3'
};
this.overrideSettings = function(settings){
var len = settings.length, i;
if(len){
for(i = 0; i < len; i++){
defConfig = $.extend(defConfig, settings[i]);
}
}else{
defConfig = $.extend(defConfig, settings);
}
alert(JSON.stringify(defConfig));
}
};
window.instance = new plugin();
})(jQuery,window);
//File 1.js
window.instance.overrideSettings({d:'93'});
//File2.js
window.instance.overrideSettings({b:'22222'});