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

mobify-plugin

v4.0.0

Published

A generic plugin factory method for creating Mobify plugins

Downloads

40

Readme

Mobify Plugin Factory

A generic plugin factory method for creating Mobify plugins.

NPM Circle CI

Requirements

Installation

The plugin factory can be installed using NPM:

npm install mobify-plugin

Usage with Require.js

To use with require.js, after installing through NPM you merely have to reference the plugin factory in your require config file:


{
    'paths': {
        'plugin': 'node_modules/mobify-plugin/dist/plugin.min',
        'myPlugin': 'plugins/myPlugin'
    }
}

And then require the plugin factory in as needed:

define(
    ['$', 'plugin'],
    function($, Plugin) {
        // create plugin
    }
);

Usage

The plugin factory requires a few things to be defined in your plugin prior to calling the plugin factory function.

  1. Your plugin's constructor, calling __super__
  2. A DEFAULTS static property on your plugin's constructor
  3. A VERSION static property on your plugin's constructor

Creating your plugin

Let's look at an example. In this example, we're going to create a button plugin. To do so, we will use the following code:

define(
	[
		'$',
		'plugin'
	],
	function($, Plugin) {
		function Button(element, options) {
			Button.__super__.call(this, element, options, Button.DEFAULTS);
		}

		Button.VERSION = '0.0.1';

		Button.DEFAULTS = {
			cssClass = 'button'
		};

		Plugin.create('button', Button, {
			_init: function(element) {
			}
		});
	}
)

First, we declare a Button constructor, and VERSION and DEFAULTS properties. We then invoke the static Plugin.create function. Through prototypal inheritance, this function extends the Button prototype with the Plugin prototype. Additionally, it creates our jQuery plugin interface.

To create a button instance, you merely need to use:

$('<button />').button();

The Plugin factory method

Extends a plugin using the Plugin prototype.

| Parameter name | Description | |----------------|-------------| | name | The name of the plugin, in lowercase. | | ctor | The constructor of the plugin we want to extend. | | prototype | Additional methods we want to extend onto our plugin's prototype. The prototype must declare an _init function, which is used for plugin construction. |

See the example above for usage.

Invoking methods on a plugin.

The plugin factory facilitates invoking methods via the plugin interface. This means that once a plugin is initialized, public methods can be invoked by passing the name of the method as the first parameter to the plugin function.

Public methods are methods defined on the object passed into the Plugin.create factory method that aren't preceded by an underscore character. Methods preceded by an underscore are considered private methods.

Using our button example above, here's what public methods would look like:

define(
	[
		'$',
		'plugin'
	],
	function($, Plugin) {
		function Button(element, options) {
			Button.__super__.call(this, element, options, Button.DEFAULTS);
		}

		Button.VERSION = '0.0.1';

		Button.DEFAULTS = {
			cssClass = 'button'
		};

		Plugin.create('button', Button, {
			_init: function(element) {
				this.$element = $(element);
			},
			enable: function() {
				this.$element.removeAttr('disabled');
			},
			disable: function() {
				this.$element.attr('disabled', 'disabled');
			},
			isEnabled: function() {
				return !this.$element[0].hasAttribute('disabled');
			}
		});
	}
)

In the above example, the enable and disable functions are public. To invoke the method, simply pass the method name into the plugin function:

var $button = $('<button />').button();

$button.button('disable');

Method return values

It's important to note that there's some specific behaviour around invoking methods that return a value when using a single element vs. a set of elements.

When invoking a method against a single element, and when that method returns a value, the value will be returned as expected.

var $button = $('<button />').button();

var enabled = $button.button('isEnabled'); // returns true

When invoking a method against a set of elements, and when that method returns a value, the original set of elements will be returned.

var $buttons = $('.lots-of-buttons').button();

var enabled = $buttons.button('isEnabled'); // returns original set of elements

This behaviour is intentional, as it's assumed that it's unlikely to be calling methods against a set of elements when expecting primitive values in return.