npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jquery-plugin-generator

v1.4.1

Published

jQuery plugin generator from classes / functions

Downloads

33

Readme

jquery-plugin-generator

NPM version Build Status

jQuery plugin generator from classes / functions

Install

Install with npm:

$ npm install jquery-plugin-generator --save

Usage

Create plugin

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 */);

Call plugin

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

Calling plugin on same element multiple times

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

Getting class/function instance from element

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"

API

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.

Plugin generator options

| 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 |

Running tests

Install dev dependencies:

$ npm install -d && npm test

License

Copyright © 2020, Kaspars Zuks. Released under the MIT license.