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

v0.0.9

Published

A Flux dispatcher for using with Backbone

Downloads

6

Readme

Backbone Dispatcher

Build Status Code Climate

Extension for using Flux architecture with Backbone instead of ReactJS.

╔════════════╗       ╔════════════════════╗       ╔═══════╗
║ Dispatcher ║──────>║ Models/Collections ║──────>║ Views ║
╚════════════╝       ╚════════════════════╝       ╚═══════╝
     ^                                                │
     └────────────────────────────────────────────────┘

Installing

Via npm

	npm install backbone-dispatcher --save

or via Bower

	bower install backbone-dispatcher --save

Usage

	var dispatcher = new Backbone.Dispatcher({
			actions: ['ACTION_1']
		});

	// It can also be a Collection
	var MyModel = require('./MyModel');
	var myModel = new MyModel();

	// When ACTION_1 is dispatched, pass the payload for myModel.methodName()
	// If doesn't pass the third argument, it will call myModel.ACTION_1()
	dispatcher.register('ACTION_1', myModel, 'methodName');

	// You can dispatch like this
	dispatcher.ACTION_1('Hi, I\'m the payload');
	dispatcher.dispatch('ACTION_1', 'Hello!');

	// Now you can make your views listen to your store (Model or Collection)

API

  • extend([options]): Static method, let's you extend the Dispatcher (check the examples below).
  • createAction(actionName/actionObject[, beforeEmit/actionCallbacks]): Instance method, creates a new action. Callbacks:
    • shouldEmit(payload): Returns true if should emit, and false if not.
    • beforeEmit(payload, next): Run right after shouldEmit, pass the changed payload as parameter to next.
  • createActions(arrayOfActions): Instance method, receive an array of methods that are passed to createAction().
  • dispatch(actionName, payload): Instance method, dispatches an action.
  • <actionName>(payload): Instance method, dispatches the action 'actionName'.
  • register(actionName, model/collection[, callback]): Makes the model/collections listen to actionName, and will call either callback(payload) (if callback is a function), or model[callback](payload)/collection[callback](payload) (if callback is a string corresponding to a model/collection's method name) when dispatched. If callback is not passed, it will be the same as actionName (so a model[actionName](payload) would be executed).
  • registerStore(actionNamesArray, model/collection[, callbacks]): Calls register (see the line above) for each pair action/callback. callbacks can also be a string or a function: in this case, all the actions will be bound to such unique callback.

Examples


	var MyDispatcher = Backbone.Dispatcher.extend({
			initialize: function() {
				console.log('I\'m the constructor here!');
			}
		});

	// You can create actions in the constructor
	var dispatcher = new MyDispatcher({
			actions: [
				'action_1',
				{
					name: 'action_2',
					beforeEmit: function(payload, next) {
						console.log('Hey, I\'m emiting ' + payload + '!');
						next(payload);
					},
					shouldEmit: function(payload) {
						if(payload < 2) {
							console.log('Sorry, son, I can\'t emit ' + payload);
							return false;
						}
						console.log('Aaw yeah, just emiting ' + payload + '!');
						return true;
					}
				}
			]
		});

		// Or like this
		dispatcher.createAction('action_3');

		// Or like this
		dispatcher.createAction('action_4', function(payload, next) {
			console.log('So... this is the beforeEmit!, I\'m gonna double it for you.');
			next(payload * 2);
		});

		// Or like this
		dispatcher.createAction('action_5', {
			beforeEmit: function(payload, next) {
				console.log('Hey!');
				next(payload);
			},
			shouldEmit: function(payload) {
				console.log('I would say that you shall not pass, but c\'mon...');
				return true;
			}
		});

		// Or like this
		dispatcher.createActions([
			'action_6',
			'action_7'
		]);

		var MyCollection = require('./MyCollection');
		var MyModel = require('./MyModel');

		var myModel = new MyModel();
		var myCollection = new MyCollection();


		dispatcher.register('action_1', myCollection, 'handleAction1');
		dispatcher.registerStore(['action_2'], myModel, ['handleAction2']);
		dispatcher.register('action_2', myCollection, function shinyCallback() {
    			console.log('Hi. This is action_2\'s inline callback! I am bound to myCollection so I can also output things like ' + this.toJSON());
    		});
		dispatcher.registerStore({ action_3: 'handleAction3' }, myModel);

		dispatcher.dispatch('action_1', 'Yep, that\'s it, I am the payload');
		dispatcher.dispatch('action_2', 3);