acanimal / jquery-plugin-boilerplate Goto Github PK
View Code? Open in Web Editor NEWBoilerplate code to create custom jQuery plugins
Boilerplate code to create custom jQuery plugins
Although this one of the most sane ways of doing jQuery plugins, there is duplication there for the getters, what do you think of another convention by using get prefix in names of getters so that you automatically introspect and export them automatically instead of retyping it in the getters ?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.