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

trigger

v1.1.1

Published

Rich, declarative, custom events made easy! Don't just listen for 'click'.

Downloads

145

Readme

The easiest way to use rich, declarative custom events and clean up your event handling!

Download: trigger.min.js or trigger.js
NPM: npm install trigger
Bower: bower install trigger

Documentation

It's pretty simple; you add a trigger="foo"" to an element, when the user "pulls it" (click or Enter keyup, as appropriate), your custom event will fire automatically. You no longer have to manually translate clicks and keyups in your app code. Your HTML becomes more readable and so does your javascript.

If that's not enough, doing trigger="validate save" will trigger the "validate" and "save" events in sequence. Your list of events can be as long as you like. To stop the sequence, catch an event in it and call event.preventDefault() or return false; to prevent the rest of the events in the list from happening.

But wait, there's more! You probably want to distinguish your battleship's "explode" event from a mere missile's "explode". Just add a namespace like so: trigger="explode.ship". Your explosion listener can check the event.namespace property.

Or perhaps you want to throw in a little contextual data: trigger="addTax['CA']" As you guessed, it gets JSON parsed and stuck at event.data.

You can even add simple tags to your events: trigger="yell#once#loud" (see event.tags and each event[tag]).

Be warned, if you feel crazy enough to use awful (but rich and declarative) combinations of all three: trigger="yell.player['howdy!']#loud", then you must put them in that namespaces, data, tags order.

Examples

<div id="#chutesAndLadders">
 <input type="dice" name="roll">
 <button trigger="move#up nextPlayer">Climb</button>  
 <button trigger="move#down nextPlayer">Slide</button>
</div>
var game = document.querySelector('#chutesAndLadders');
game.addEventListener('nextPlayer', function() {
    player = player.next;
});
game.addEventListener('move', function(e) {
   var distance = game.querySelector('[name=roll]').value;
   if (e.up) player.climb(distance);
   if (e.down) player.slide(distance);
   if (player.hasWon()) e.preventDefault();//blocks nextPlayer event
});

If you, for some strange reason, care about "valid HTML", then you can do this:

trigger._.attr = 'data-trigger';
<button data-trigger="foo">Foo!</button>

But personally, I don't recommend it.

Release History

  • 2010-04-02 v0.1 (internal release - jQuery plugin)
  • 2012-09-13 v0.3 (internal release - declarative tags and data)
  • 2013-05-03 v0.9.0 (public) - First GitHub release (sans jQuery integration)