If I were you, I would resist the temptation to do too much 'correctness'
checking if it is a plugin that you are releasing to the general public.
The reason I say that is sometimes it is desirable to add extra things in to
your configuration that the plugin doesn't expect, so that it gets stored in
context.  To allow that to happen, don't halt execution if you find
something you didn't expect, just log it.

(function($){
  // list all of your expected options and their default values here
  var defaults = {};
  var validateOptions = function(o) {
      for ( var i in o ) {
        if ( ! defaults.hasOwnProperty(i) ) console.log( 'MyPlugin:
unexpected option: ' + i );
      }
    };
  var MyPlugin = function(o) {
      validateOptions(o);
      o = $.extend({},defaults,o);
      ...
    };
})(jQuery);

Also, once you have everything debugged, I would comment out the call to
validateOptions, so that production code is not generating console output,
since that may generate errors for some users, and does not serve anyone but
the developer.


On Wed, Jan 13, 2010 at 6:24 AM, SamCKayak <s...@elearningcorner.com> wrote:

> Passing parameters using an object has become a popular method among
> jQuery add-ins.  Few plugins actually validate the passed parameter
> object to ensure that elements are spelled correctly.
>
> Most plugins use the jQuery.extend method which would copy the passed
> parameter { resiz: 1 } just as easily as the correctly spelled
> parameter { resize: 1 }.  A validation of the object before or after
> the $.extend would ensure there were no misspellings, something like:
>
> function( arg1, arg2, oParms ) {
>  oParms = $.extend( { resize: 0, height: 100, width: 100 }, oParms )
>  // $.notIn is fictitious, returns true if any element of oParm is
> not in oAllParms
>  if ( $.notIn( oAllParms, oParms ) { some error notification here }
>  // We know the parms are good so proceed here
>
> Before inventing the wheel again, can anyone point to a clean example
> of a plugin that validates object parameters?
>



-- 
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com
--
http://www.irie-inc.com/
http://arrowwood.blogspot.com/

Reply via email to