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

v1.6.0

Published

Declarative jQuery plugin initialization on DOM elements

Downloads

261

Readme

jquery-app

NPM version Build Status

Declarative jQuery plugin initialization on DOM elements. Finds elements, which has data-plugin attribute and calls jQuery plugins on them passing settings/options as first argument.

On repeated .app() calls if plugin already was initialized on the element then it will be ignored.

Install

Install with npm or yarn:

$ npm install jquery-app --save
$ yarn add jquery-app

Usage

List of plugins which will be called are defined by settings data-plugin="PLUGIN_NAMES" attribute on elements. Plugin names are either comma or space separated list of plugin names.

<!-- data-PLUGIN_NAME="JSON_CONFIGURATION" -->
<input
    type="text"
    data-plugin="datepicker"
    />

Plugin settings

Settings/options for plugin can be set either using data-PLUGIN_NAME="JSON_CONFIGURATION" attribute

<!-- data-PLUGIN_NAME="JSON_CONFIGURATION" -->
<input
    type="text"
    data-plugin="datepicker"
    data-datepicker='{"numberOfMonths": 3, "showButtonPanel": true}'
    />

or specific data-PLUGIN_NAME-PROPERTY="VALUE" attribute

<input
    type="text"
    data-plugin="datepicker"
    data-datepicker-number-of-months="3"
    data-datepicker-show-button-panel="true"
    />

In this example under the hood jquery-app will call plugin passing options as

$(INPUT_ELEMENT).datepicker({
    "numberOfMonths": 3,
    "showButtonPanel": true
});

Call jquery-app to initialize plugins

$(function () {
    // Find all elements with data-plugin attribute inside body and initialize plugins
    $('body').app();
});

In some cases you may want to use different attribute name than data-plugin, that can be done using namespace property when calling $.fn.app

$(function () {
    // Find all elements with data-attach attribute inside body and initialize plugins
    $('body').app({
        'namespace': 'attach'
    });
});

Multiple plugin support

Multiple plugins can be defined using space or comma separated list and since options are prefixed there won't be collision between options/settings for each plugin.

<div
    data-plugin="pluginA, pluginB"
    data-pluginA-option-a="alpha"
    data-pluginA-option-b="beta"
    data-pluginB-option-a="gamma"
    data-pluginB-option-c="delta"
    ></div>

Under the hood jquery-app will call plugins as

$(DIV_ELEMENT).pluginA({
    "optionA": "alpha",
    "optionB": "beta"
});
$(DIV_ELEMENT).pluginB({
    "optionA": "gamma",
    "optionC": "delta"
});

API

$.app.settings / $.fn.app([settings])

Options

| Name | Type | Usage | Default | | -------- | ------- | ---------------------------------------- | -------- | | namespace | String | Data attribute name using which list of plugin names are defined | "plugin" | | namespaceOptions | Boolean | Pass to plugin only data from attributes starting with plugin name, eg. data-datepicker. If set to false all data is passed to plugin as options and without removing prefixes | true | | debug | Boolean or String | Output all successful and failed plugin initialization calls to the console. If value is "error" then output only failed plugin initialization calls | false |

namespaceOptions

Using namespaceOptions: true (default)

<div data-plugin="datepicker" data-datepicker-number-of-months="3">

Using namespaceOptions: false

<div data-plugin="datepicker" data-number-of-months="3">

Running tests

Install dev dependencies:

$ npm install -d && npm test

License

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