Ryan Ryan - 8 months ago 23
jQuery Question

Having a set of default options to be passed to multiple initializers

I'm building an app that allows users to select colors that will be applied to css. I'm using spectrum.js for the color picker. This is how you initialize the plugin:


You can pass in many options and I'm wanting to know if it is possible to make a global 'initializer' (that might be the wrong term) with a few default options that other 'initializers' can inherit from.

If so, how would it be done?


Of course you can.

var defaultOptions = {color: '#0', flat: true};

Now you can merge this default option with any local set of options and pass it to an initializer.

var myOptions = {showInput: true};

$("#selector").spectrum(meargeObjects(defaultOptions, myOptions));

This answer provides a way to write the meargeObjects function.