Creating Your Very Own jQuery UI Plugin Defaults - J Query

Problem
Every time you create a jQuery UI dialog, you find yourself specifying the same few options, among others:

You long for your code to be as succinct as it once was. What happened to the simple beauty of $('#msg').dialog();?

Solution
Override the plugin defaults before init by extending $.ui.pluginname.defaults:

Discussion
If you were only looking to improve the readability a bit, you could simply put the options in a variable and pass them to the plugin init:

But this recipe is about more than just readability and code beauty. It’s about changing the default behavior of a plugin you didn’t write. Plus, it makes it so you can get back to the simple no-options init:

As Dave Methvin famously said, “It could n’t get any shorter unless it read your mind.”
Of course, you still have the option of overriding even these custom defaults by passing custom options to the plugin init, as in the previous recipe.
Don’t forget that plugin options are cloned and extended from the defaults at the time of in it. So, extending $.ui.dialog.defaults after a <div>has already been initialized as a dialog will have no effect on that dialog, even if that in it was done with no custom options. The effect will be on any dialogs initialized after the defaults were overridden.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics