Giter Club home page Giter Club logo

onmount's Introduction

onmount

Run something when a DOM element appears and when it exits.
No dependencies. Legacy IE compatible. 1kb .min.gz.

Status

Overview

Detecting elements

Run something to initialize an element on its first appearance.

onmount = require('onmount')

onmount('.push-button', function () {
  $(this).on('click', function () {
    alert('working...')
  })
})

Polling for changes

Call $.onmount() everytime your code changes.

$('<button class="push-button">Do something</button>')
  .appendTo('body')

$.onmount()

$(".push-button").click()  //=> 'working...'

jQuery integration

jQuery is optional; use it to poll on popular events.

$(document).on('ready show.bs closed.bs load page:change', function () {
  $.onmount()
})

Cleanups

Supply a 2nd function to onmount() to execute something when the node is first detached.

$.onmount('.push-button', function () {
  /*...*/
}, function () {
  alert('button was removed')
})

document.body.innerHTML = ''

$.onmount() //=> 'button was removed'

What for?

Onmount is a safe, reliable, idempotent, and testable way to attach JavaScript behaviors to DOM nodes. It's great for common websites that are not Single-Page Apps. Read more on its premise and motivation.

rsjs (Reasonable System for JavaScript Structure) is a great standard that onmount fits perfectly into.

Usage

Onmount is available via npm and Bower.

npm install onmount
bower install onmount

It can be used as a CommonJS module or on its own. It doesn't require jQuery, but if jQuery is found, it'll attach itself to it as $.onmount.

onmount = require('onmount')    // With CommonJS (ie, Browserify)
window.onmount                  // with no module loaders:
$.onmount                       // with jQuery

API

onmount()

Runs all behaviors.

When used with jQuery, this can be passed as an event handler, eg, $(onmount) or $(document).on('load', onmount).

onmount(selector)

Runs all behaviors registered for selector.

onmount(selector, init())

Registers a behavior for selector to run the callback init().

onmount(selector, init(b), exit(b))

Registers a behavior for selector to run the callback init(). The exit() callback will be called once the behavior is triggered again but the element is no longer attached to the DOM.

The callbacks are passed an object b, and the same object is passed for both init and exit. This allows them to communicate and keep aware of state. A string ID is also provided, b.id, which is guaranteed unique for every behavior-element pair.

onmount.teardown()

Forces the teardown of all currently-applied behaviors. This triggers the exit handlers for all behaviors currently on the page.

onmount.reset()

Clears all defined behaviors. Useful for tests.

onmount.observe()

Automatically invoke when new DOM elements appear using MutationObserver API. Returns true if it succeeds.

onmount.unobserve()

Turns off observation previously turned on via onmount.observe().

onmount.debug

Set this to true to see debug messages.

Browser compatibility

All modern browsers and IE8+. For legacy IE, use it with jQuery 1.x.

Examples

Examples are available in the source repo. See examples →

Thanks

onmount © 2015+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.