Git Product home page Git Product logo

events-5's Introduction

events

Higher level dom event management with direct and delegate event handling support.

This component makes subscription management easy and unobtrusive since it does not muck with your view prototypes. Unbinding to "clean up" after your view is as simple as invoking this.events.unbind(), or more specific unbinds may be performed.

It's design to work with a "host" object, typically a view, that provides callbacks, making callback management much less tedious than libraries like jQuery.

Installation

$ component install component/events

Example

var events = require('events');
var el = document.querySelector('.user');

var view = new UserView(el);

function UserView(el) {
  this.events = events(el, this);
  this.events.bind('click .remove', 'remove');
  this.events.bind('click .hide', 'hide');
}

UserView.prototype.remove = function(){
  // remove the user
  this.hide();
};

UserView.prototype.hide = function(){
  // hide the view
};

UserView.prototype.destroy = function(){
  // clean up anything you need to
  this.events.unbind();
};

API

Events(el, obj)

Initialize a new events manager targetting the given element. Methods are delegated to obj.

Events#bind(event, [method])

Bind direct event handlers or delegates with event and invoke method when the event occurs, passing the event object. When method is not defined the event name prefixed with "on" is used.

For example the following will invoke onmousedown, onmousemove, and onmouseup:

events.bind('mousedown')
events.bind('mousemove')
events.bind('mouseup')

Alternatively you may specify the method name:

events.bind('click', 'toggleDisplay')

To use event delegation simply pass a selector after the event name as shown here:

events.bind('click .remove', 'remove')
events.bind('click .close', 'hide')

You may bind to the same element with several events if necessary, for example here perhaps .remove() does not manually invoke .hide():

events.bind('click .remove', 'remove')
events.bind('click .remove', 'hide')
events.bind('click .close', 'hide')

Addition arguments are passed to the callee, which is helpful for slight variations of a method, for example sorting:

events.bind('click .sort-asc', 'sort', 'asc')
events.bind('click .sort-dsc', 'sort', 'dsc')

Events.unbind([event], [method])

There are three flavours of unbinding -- you may unbind all event handlers, all specific to event, or all specific to event and the given method. For example these are all valid:

events.unbind('click', 'remove')
events.unbind('click', 'hide')
events.unbind('click')
events.unbind()

License

MIT

events-5's People

Contributors

tj avatar tootallnate avatar ockham avatar jonathanong avatar yields avatar cristiandouce avatar amelon avatar forbeslindesay avatar juliangruber avatar

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.