boost-js
A jQuery plugin generator with a few tricks up its sleeve.
Boost JS
A jQuery plugin generator with a few tricks up its sleeve.
Installation
Install with npm:
npm install boost-js
Install in browser:
Usage
Create Plugin
var boost = ;// var boost = $.fn.boost; (browser install) { // reserved attributes (see below for details) thissource; thisid; thissettings; thisreferences; thisroles;}MyPluginprototype = ...; $fnmyplugin = ;
Call Plugin
;
Reserved Attributes
There are 5 reserved attributes which you can reference within your plugin's constructor.
this.source
The element used to initialize your plugin.
this.id
The value of the source element's id
if present.
this.settings
Your plugin's settings are collected from three places, in order of priority:
-
@creation (defaults)
$fnmyplugin =; -
@data-attribute (on source element)
-
@instantiation
;
this.references
You can easily associate any element with your plugin by referencing the id in a href
or data-bind
attribute.
click meclick me
var { thisreferences;}
this.roles
You can also group your references by role with the data-role
attribute.
click me
var { thisroles'trigger';}
Add multiple roles to a single element by separating them with a pipe, i.e. data-role="foo|bar"
.
API
boost( fn, [options] )
fn
will be called using a new keyword for each element the plugin is called for. [options]
is an object literal which will define the default settings for every instantiation of the plugin.
var {...}$fnmyplugin = ;
boost.auto()
You can instantiate any plugin directly from the HTML after running the boost.auto
method. It should only be run once and after all plugins have been defined.
var boost = ;// define plugins here ...boost;
Now you can initialize directly from an element via the [data-init]
attribute.
$().myplugin( [options] )
Your plugin will be instantiated on every element in the set, with the options
overriding the values defined in the boost()
method. Will return array if more than one instance is created.
var inst = ;
$.fn.myplugin.init( [elems], [options] )
You can also access the init method directly to instantiate a plugin manually.
var inst = $fnmyplugin;
$.fn.myplugin.instances
Each instance for a plugin is stored in an object. Boost JS uses the instance's id or it's position in the object as the key.
var someInstance = $fnmyplugininstancessomeId;
$.fn.myplugin.defaults
The default options for the plugin; the same as was passed in at creation.
var defaultSettings = $fnmyplugindefaults;
Running Tests
$ npm install && npm test
License
Copyright © 2016, Mark McCann. Released under the MIT license.