
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
jquery-plugin-generator
Advanced tools
jQuery plugin generator from classes / functions
Install with npm:
$ npm install jquery-plugin-generator --save
var generate = require('jquery-plugin-generator');
function MyPlugin ($element, options) {
this.options = $.extend({'firstName': '', 'lastName': ''}, options);
console.log('constructor: ' + this.options.firstName + ' ' + this.options.lastName);
}
MyPlugin.prototype = {
update: function (firstName, lastName) {
console.log('update: ' + firstName + ' ' + lastName);
}
};
$.fn.myplugin = generate(MyPlugin /* function or ES6 class */, {} /* plugin generator options */);
jQuery plugin can be called on the same element multiple times, but constructor (MyPlugin) will be called on each element only once.
Constructors first argument always will be jQuery element, all other arguments will be passed to the constructor as is.
$('div').myplugin({'firstName': 'John', 'lastName': 'Doe'});
//console => constructor: John Doe
Call MyPlugin API method by passing function name as first argument, rest of the arguments will be passed to the function unchanged
$('div').myplugin('update', 'Jane', 'Doe');
//console => update: Jane Doe
Calling API method on element for first time will first call constructor and then API method.
Constructor will be called with options empty.
$('div').myplugin('update', 'Jonathan', 'Doe');
//console => constructor:
//console => update: Jonathan Doe
Constructor is called only once, but to allow plugins to change options or do something else on subsequent calls you can implement setOptions method.
setOptions will be called if constructor has already been called on the element and it will the same arguments with which plugin was called.
If function or class method doesn't have this method, then will fail silently.
You can specify different method name by passing optionsSetter option to the generator.
function MyPlugin ($element, options) {
this.options = $.extend({'firstName': '', 'lastName': ''}, options);
console.log('constructor: ' + this.options.firstName + ' ' + this.options.lastName);
}
MyPlugin.prototype = {
update: function (options) {
console.log('update: ' + options.firstName + ' ' + options.lastName);
}
};
$.fn.myplugin = generate(MyPlugin, {'optionsSetter': 'update'});
$('div').myplugin({'firstName': 'John', 'lastName': 'Doe'});
//console => constructor: John Doe
$('div').myplugin({'firstName': 'Jonathan', 'lastName': 'Doe'});
//console => update: Jonathan Doe
To get class/function instance call myplugin('instance') API method.
If plugin is not called on element before then it will return null.
// Plugin hasn't been called on element before, will return null
$('div').myplugin('instance'); // => null
// Call plugin on element
$('div').myplugin({'firstName': 'John', 'lastName': 'Doe'});
//
$('div').myplugin('instance'); // => MyPlugin {options: {...}}
$('div').myplugin('instance').options.firstName // => "John"
generator(fn, [options])fn Function or ES6 class, which will be called using new keyword for each element plugin is called for. As first argument will be passed jQuery element, all following arguments will be same as they were used when calling a plugin.
| Name | Type | Usage | Default |
|---|---|---|---|
| api | Array | List of method / function names, which are accessible using .myplugin('apiMethodName') By default all methods are accessible | null |
| optionsSetter | String | Method name. If plugin has already been initialized, then calling plugin again on same element will trigger method with this name | setOptions |
Install dev dependencies:
$ npm install -d && npm test
Copyright © 2020, Kaspars Zuks. Released under the MIT license.
FAQs
jQuery plugin generator from classes / functions
The npm package jquery-plugin-generator receives a total of 111 weekly downloads. As such, jquery-plugin-generator popularity was classified as not popular.
We found that jquery-plugin-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.