0

I'm writing a jQuery plugin using A Highly Configurable And Mutable Plugin Pattern (Until now I've nevere created a "complete" jQuery plugin with these design patterns)

Now, I'd like to create a plugin where the options can be extended as data attributes like:

<div class="my-wrapper" data-option="myoption" [...]>

or as a collection like:

$(document).ready(function(){
    $('.my-wrapper').myPlugin({
        option: 'another option',
        [...]
    });
});

as you can see, this is a little different configuration as the one described here (you have to scroll down a little to find the pattern I'm using) This is the $.extend code line:

$.extend( {}, this.defaults, this.options, this.metadata );

But what happens if data-option is missing? well simply option === undefined because I'm declaring this.metadata as

this.metadata: {
    option: this.$element.data('option')
} 

How can I avoid this?

Should I "check" this.metadata in order to remove the undefined attributes or simply change the $.extend line to

$.extend( {}, this.defaults, this.metadata, this.options );

Or, for example, throw an exception like "option is missing"? I think there may be too many answer to this question, but I simply want to avoid that a "default" option is missing or undefined if the user does not set it as a data attribute.

Thanks everyone!

IlGala
  • 3,331
  • 4
  • 35
  • 49

0 Answers0