javascript - Override settings of jQuery plugin? -
i've built plugin :
;(function ($,window,undefined){ ... ...default settings area... ...content area... ... })(jquery,window);
but plugin can have many(!) configurations .(files configurations)
so each configuration file can in js file . example :
mysettings.js :
var mysetings= {a:1,b:2.....}
so problem :
the settings file must loaded before plugin. ( plugin able read
mysettings
in override settings area)the way communicate
mysettings
plugin viawindow
object
question :
what correct way configure plugin can have many(!) settings via js file.
as don't have code example work i'll give generalized solutions.
- collect settings object(s) in array , iterate on them within override settings area.
- expose plugin via window , directly append settings there.
example 1
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; < len; i++){ defconfig = $.extend(defconfig, settings[i]); } }else{ defconfig = $.extend(defconfig, settings); } alert(json.stringify(defconfig)); }; var instance = new plugin(window.settings || []); })(jquery,window);
example 2
//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; < 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'});
Comments
Post a Comment