Greetings, using your template, we needed a method to return values.
For example,
var myvalue = $('#foo').myPlugin('methodThatShouldReturnValue') ;
As written here, this is not allowing the return value in methodThatShouldReturnValue to pass through:
// Invoke the specified method on each selected element
return this.each(function() {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
});
It does not allow return value of the method to be returned, instead 'return this.each...' is only return jQuery object.
I've altered it like this, but perhaps you have a more elegant solution:
// Invoke the speficied method on each selected element
var results ;
this.each(function() {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
results = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
});
return results ;
Note, in our plugin, that method is only applied to one id on the dom, not multiple.
In the case of multiple return values required, that code could be written like this:
// Invoke the speficied method on each selected element
var results = [] ;
this.each(function() {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
results.push( instance[options].apply(instance, Array.prototype.slice.call(args, 1)) ) ;
}
});
return results ;
Again, perhaps you have a preferred way to rewrite the code to account for both scenarios and return appropriately.