jquery-boilerplate / jquery-patterns

A variety of jQuery plugin patterns for jump starting your plugin development
1.59k stars 242 forks source link

Exposing defaults as a property of $.fn.pluginName #8

Closed wheresrhys closed 4 weeks ago

wheresrhys commented 12 years ago

I was about to apply your basic plugin boilerplate to my plugins as I think in general it's neater than what I've been doing, but there is one thing I don't like; namely that the defaults are not exposed publically so that a developer can change the default settings for the plugin (eg if a plugin defaults to autoplay: false, but on the developer's site most instances use autoplay:true it makes sense for them to over-ride the default globally). Of course, the developer coudl go in and directly edit the plugin, but I never like doing this as it's not obvious to future developers that they can't just overwrite the plugin with updated versions from the plutgin's author.

I think this would need to be done by getting rid of the defaults variable and using $.fn[pluginName].defaults instead.

addyosmani commented 12 years ago

@wheresrhys The repo actually has another pattern called the 'best options' pattern which tries to solve the problem you've described above. The reason I guess I kept it separate was so that developers wishing to use that pattern could easily modify the basic boilerplate accordingly, but it would be interesting to see if others feel the basic should cover this too. I don't mind at all dropping patterns if they end up being integrated with the base/basic if that's the case.

yckart commented 11 years ago

What about an extra chapter in the wiki about "how to handle the plugin defaults and predefinitions". According to this problem, extending the plugin defaults object with the options-parameter in our plugin-wrapper if !(this instanceof $), provides the most simple way to accomplish this problem.

Update: https://github.com/jquery-boilerplate/boilerplate/wiki/Handling-plugin-defaults-and-predefinitions