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

v0.2.4

Published

Super simple, lightweight and solid factory of jQuery plugins

Downloads

16

Readme

jQuery Factory

Build Status npm version bower version Codacy Badge Dependency Status

По-русски

Super simple, lightweight and solid factory of jQuery plugins. It allows to follow classic JavaScript patterns instead of jQuery's while creating plugin.

Features

  • Support all modern browsers (including mobile browsers)
  • Support Internet Explorer 7-8 (needs jQuery 1.8 or older)
  • Support jQuery version from 1.6
  • Support Zepto (needs data module)
  • Around 600 bytes compressed
  • Efficient code re-usage when writing several plugins
  • Support requirejs/webpack and amd
  • Test mode

Install

Bower

bower install --save jquery-factory

Npm

npm install --save jquery-factory

Usage

Usage with requirejs or webpack

var $ = require('jquery')(window),
    newPlugin = require('jquery-factory')($);

Usage in browser

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="/path/to/newPlugin.jquery.js"></script>

Plugin creation $.newPlugin(pluginName, Constr, callback)

Produces new jQuery plugin in $.fn object with Constr function. Factory accepts string pluginName. If plugin with the same name is exists factory throws an error.

pluginName — name of creating plugin. Should not contain name of existing plugins and internal jQuery methods

Constr — constructor Function for new plugin.

callback — callback function (deprecated)

Constructor

Constr takes $element, options and htmlData arguments.

$element contains jQuery oblect of current element

options has any data passed while plugin init

htmlData has value of data-<pluginname> attribute (for example <span data-plugin="myText"></span>).

Methods

By default each created plugin has built-in init, update and destroy methods that could be redefined.

init method should contain event handlers attaching, initial data, adding classes, etc.

update method using for options update and changing instance state. Method will be called if plugin instance was already created on element.

destroy method for final destroying: handlers unattaching and plugin instance removing (using .removeData).

You can append any method by adding it to prototype of constructor function.

Instances

Plugin instance stores with jQuery .data method so you can get it for testing or other purposes.

You can enable test mode by giving callback argument to $.newPlugin. callback accepts plugin instance context and should return true to continue instance attaching or false to prevent it.

To check accessory of $.fn.pluginName use __constr__ property with Constr to check plugin accessory:

$('.element').data(pluginName) instanceof $.fn.pluginName.__constr__

Examples

Empty plugin

Plugin that does nothing:

(function($) {
  var Plugin = function() {}
  $.newPlugin('plugin', Plugin);
})(jQuery);

Usage:

$('.element-set').plugin();

Element and option

Plugin accepts option and attached element. The opt class adding when attaching plugin.

(function($) {
  var Plugin = function($el, opt) {
    this.$el = $el;
    this.opt = opt;
    
    this.$el.addClass(opt);
  }
  $.newPlugin('plugin', Plugin);
})(jQuery);

Usage:

$('.element-set').plugin('some-class');

init, update, destroy methods and events

(function($) {
  var clickHandler = function(e) {
    var self = e.data;
    e.preventDefault();
    // do something
  }
  
  var Plugin = function($el, opt) {
    this.$el = $el;
    this.opt = $.extend({
      text : ''
    }, opt);
    
    this.init();
  }
  
  // init handlers
  Plugin.prototype.init = function() {
    this.$el
      .on('click', this, clickHandler)
      .text(this.opt.text);
  }
  
  // updating text option
  Plugin.prototype.update = function(opt) {
    $.extend(this.opt, opt);
    this.$el.text(this.opt.text);
  }
  
  // destroy method
  Plugin.prototype.destroy = function() {
    this.$el
      .off('click', clickHandler)
      .removeData('plugin');
  }
  
  // custom method
  Plugin.prototype.smartMove = function() {
    this.$el
      .toggleClass('smart-class');
  }
  
  $.newPlugin('plugin', Plugin);
})(jQuery);

Usage:

// creating
$('.element-set').plugin({
  text : 'This is new instance of "plugin"'
});

// updating
$('.element-set').plugin('update', {
  text : 'blah!'
});

// updating (other way)
$('.element-set').plugin({
  text : 'Blah-blah!'
});

// call custom method
$('.element-set').plugin('smartMove');

// destroying
$('.element-set').plugin('destroy');

More examples available in tests

Todo

  • Create plugin instances with Object.create (will loose compatibility with old browsers)
  • More tests
  • More examples
  • Adapt (maybe fork?) for BEM development process
  • ~~Сompatibility tests with Zepto~~

Contributing

You are welcomed to improve this small piece of software :)

Author

Thanks to