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

backbone.whenthen

v1.0.2

Published

When <these> then <those>

Downloads

4

Readme

backbone.whenthen

Build Status npm version

when <these> then <those>

Honestly, it's easier to show than tell.

//we have a Backbone.Events instance at our disposal in `app.vent`
app.vent.on('rolling', function(){
	console.log('Here we go:');
});

var instruct = require('backbone.whenthen');
instruct(app.vent)
    .when('event1', 'event2', 'event3')
    .then('rolling', function(){
        console.log("We're ready to roll!!");
    });

app.vent.trigger('event1'); // nothing happens
app.vent.trigger('event2'); // nothing happens
app.vent.trigger('event3'); // this will:
// output to console: 
Here we go:
We're ready to roll!!

This is especially useful in situations where you have a lot of parallel async processes going on and you want to respond to the collective completion of these processes. For instance: complex loading and bootstrapping sequences or bespoke view transitions.

installation

# bower
bower install backbone.whenthen
# npm
npm install backbone.whenthen

dependencies

  • Despite the name, backbone.whenthen does NOT have a hard dependency on Backbone, however it does expect Backbone.Events-compatible instances to control.
  • underscore is required shizzle though.

usage

backbone.whenthen can be used in node or the browser, either with:

// AMD
define([ "backbone.whenthen" ], function(instruct){
    //use `instruct` as in the examples
});
// CommonJS
var instruct = require('backbone.whenthen');
//use `instruct` as in the examples
<!-- old skool, definitely NOT recommended -->
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script> <!-- must be loaded first -->
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script> <!-- must be loaded first -->
<script src="/vendor/backbone.whenthen.min.js"></script>
//old skool, continued
var instruct = Backbone.WhenThen; // it's attached to Backbone
//or in case you don't load Backbone:
var instruct = WhenThen; // it's a global object... *shudders*

//use `instruct` as in the examples

API

The backbone.whenthen module exposes a function, which accepts a Backbone.Events(-compatible) event dispatcher and starts off an extremely simple fluent interface:

var _ = require('underscore');
var Backbone = require('backbone');
var instruct = require('backbone.whenthen');
var dispatcher = _.extend({}, Backbone.Events); // obviously any Backbone.Events dispatcher will do: Backbone.Model et cetera.
instruct(dispatcher);

.when({...String|...String[]})

You can pass as many (arrays of) event strings to when as you want, they'll be normalized:

instruct(dispatcher)
	.when('event:a', ['event:b', 'event:c'], 'event:d');

.when().then({...Function|...Function[]|...String|...String[]})

then accepts any number (and mixture) of (arrays of) strings and/or functions:

instruct(dispatcher)
	.when('event:a', ['event:b', 'event:c'], 'event:d')
	.then('event:z', ['event:y', fooFn], barFn);

Once all events registered in when have been triggered by dispatcher, all items registered with then will be either be executed (functions) or triggered (strings) in dispatcher. I.e. in the above example dispatcher will trigger event:z, event:y and call fooFn, barFn (synchronously and in order).

Example:

instruct(app.vent)
	.when('loaded:images', 'loaded:locales')
	.then('app:show:ui'); // `app.vent` triggers 'app:show:ui'

It doesn't matter in which order the when events are triggered, then will always wait until all of them have been triggered.

.when().have({Object}).then()

If you want to have a difference Backbone.Events instance triggering the completion event(s), you can use have in between:

instruct(loader)
    .when('something:ready', 'something:else:ready')
    .have(app.vent).then('app:ready');

In this case, once loader has triggered 'something:ready' and 'something:else:ready' the app.vent dispatcher will trigger 'app:ready'.

when time becomes a loop

then exposes a new when. You can go on ad infinitum.

instruct(app.vent)
	.when('foo').then(fooFn)
	.when('bar').then(barFn)
// you catch the drift

have's apply strictly to the then coming right after it, e.g.:

instruct(app.vent)
	.when('foo').have(someOther).then('bar')
	.when('baz').then('qux'); // `app.vent` triggers `'qux'`

Event relaying

Obviously backbone.whenthen can be used as a simple event translator/relayer, for translating module events for instance. If a single event is registered with when automatically all parameters passed when triggering said event will be passed to the then events/callbacks.

instruct(app.vent)
	.when('user:selection:completed')
	.have(account.vent).then('user:selected');
//yeah, yeah I know, not the best of examples

account.vent.on('user:selected', function(user){
    console.log(user);// outputs: {name:"Camille Reynders"}
});
app.vent.trigger('user:selection:completed', {
    name: 'Camille Reynders'
});

.destroy()

If you need to clean up you can destroy:

var relayer = instruct(loader);
//relayer.when(...).then(...);
// later on:
relayer.destroy();

destroy unregisters all callbacks and events, and cleans up all objects. If you try to use a destroyed instance it will throw errors:

relayer.destroy();
relayer.when('foo').then('bar'); // Error: `when` instance destroyed

License

MIT Copyright (c) 2015 Camille Reynders