PDA

View Full Version : How to 'send options' to function?



xFinaLx
09-09-2011, 08:51 PM
I'm trying to make a jQuery slider, and I want the user (the person who installs it on their site) to be able to specify options easily. I know the Nivo slider does it from within the $(document).ready() event, but I can't figure out how to do it that way. I want it to be like this:



<script>
$(document).ready(function() {
myFunction({
option1: 'value1',
option2: 'value2',
etc: 'etc'
});
});
</script>


But how do I recover this data so I can use it later?

rnd me
09-09-2011, 08:59 PM
jQuery.fn.myPlugin.config=function(cfg){
jQuery.fn.myPlugin.CF=cfg; //or manually add props to default config one-at-a-time if use input is to be validated...
return this;
};


then...


$.fn.myPlugin.config({
speed:4,
stop: false
}).init();

xFinaLx
09-10-2011, 06:14 PM
jQuery.fn.myPlugin.config=function(cfg){
jQuery.fn.myPlugin.CF=cfg; //or manually add props to default config one-at-a-time if use input is to be validated...
return this;
};


then...


$.fn.myPlugin.config({
speed:4,
stop: false
}).init();

I don't exactly understand... can you elaborate a bit?

xFinaLx
09-11-2011, 10:42 PM
I really need some help with this, anyone know how to do it?

rnd me
09-12-2011, 12:29 AM
are you making a jQuery plug-in, or just a script that uses jQuery?

you can accept options at
run time as an optional argument,
at load time as a config call or a static object being assigned to a config path,
or pre-load time as an editable section of your distributable file - often seen at the top of script files.

my example assumed a jQuery plug-in.
it could call config() inline on a regular jQuery chain, continuing to actually invoke it with typical passed arguments after the next dot.

where you store these settings is up to you, a settings path to an object is the simplest, but be sure to validate the input if this is going to be distributed...