javascript - jQuery plugin options: required, optional, inaccessible -


i'm curious how specify options jquery plugin in way required, optionally overridden, , can't touched.

i started off usual:

jquery.fn.plugin = function (options){   var defaults = { username: "", posts:10, api: "http://myapi.com" }   var settings = jquery.extend({}, defaults, options); } 

let's want username required, posts optional (defaults 10) , (you being user of plugin) can't change api, if try. ideally, they'd still in same data structure instead of being split separate objects. ideas?

if wanted username required, i'd make parameter, given it's required thing , there aren't 20 parameters want way. this:

jquery.fn.plugin = function (username, options){   var defaults = { posts:10, api: "http://myapi.com" }   var settings = jquery.extend({}, defaults, options); } 

there's not clean required way completely in single passed object, @ least not simple one, want api. alternative specify no default , alert or if it's not there suppose:

jquery.fn.plugin = function (options){   var defaults = { posts:10, api: "http://myapi.com" }   var settings = jquery.extend({}, defaults, options);   if(typeof options.username == 'undefined') {      alert("you must specify username");     return this;   } } 

edit - missed part of question, inaccessible ones, either have values hard-coded, since that's same result, or bit cleaner internal object inside plugin out of scope everywhere else. again, not trivial on same object unless kept intenaldefaults object, , called jquery.extend() after extend of settings overwrite consumer passed in.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -