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

radio.controller

v0.3.0

Published

A lightweight controller for backbone.radio.

Downloads

7

Readme

Radio.Controller

An itty-bitty, super-simple, low-drama controller component for Backbone.radio.

Installation

Install with npm:

npm install radio.controller --save

Use with Browserify:

var Controller = require('backbone.radio');

Usage

The word "controller" has been used to describe lots of different patterns in front-end architecture in the last couple years. Here, it's just a simple component that automatically binds events, commands, and requests pubished with Backbone.Radio onto callbacks. A controller could be used in lots of different ways. I like to think of it as a component that manges the lifecycle of a view - it instantiates it when the application starts, feeds it with data and triggers state changes, and tears it down when it's not needed anymore. But, there are lots of ways to skin a cat!

Events

A controller can bind to events on multiple channels. Use the same syntax you'd use to bind UI events to callbacks in a Backbone view:

Style 1: Like Backbone views

var Ctl = Controller.extend({

  events: {
    'channel1 event1': 'event1',
    'channel1 event2': 'event2',
    'channel2 event3': 'event3',
    'channel2 event4': 'event4'
  },

  event1: function() { /* .. */ },
  event2: function() { /* .. */ },
  event3: function() { /* .. */ },
  event4: function() { /* .. */ }

});

And, then, triggering the bound events will fire the callbacks.

var channel1 = Radio.channel('channel1');
channel1.trigger('event1'); // Calls Ctl.event1();

Sometimes, you want to subscribe lots of events on the same channel, and it gets annoying to duplicate the event name over and over again in the key names. If you want, you can use an alternate syntax that lets you nest a bunch of event-callback pairs under a single key that identifies the channel. So, this is equivalent:

Style 2: Nested objects

var Ctl = Controller.extend({

  events: {
    channel1: {
      event1: 'event1',
      event2: 'event2'
    },
    channel2: {
      event3: 'event3',
      event4: 'event4'
    }
  },

  event1: function() { /* .. */ },
  event2: function() { /* .. */ },
  event3: function() { /* .. */ },
  event4: function() { /* .. */ }

});

Commands and Requests

The semantics of commands and requests are a bit different. So as not to violate the single responsibity principle and incur the wrath of Robert Martin, Radio.Controller assumes that a controller only ever responds to commands or requests on a single channel. This makes sense, if you think about it - if you're writing a Map controller, it should respond to requests like map:getLonLat or map:getZoom, but not things like timeline:getDate - that's the Timeline controller's job.

Radio.Controller formalizes this by connecting to a "local" channel set on the controller, and then binding command/request callbacks on that channel. Eg:

var Map = Controller.extend({

  channel: 'map',

  commands: {
    command1: 'command1',
    command2: 'command2'
  },

  requests: {
    request1: 'request1',
    request2: 'request2'
  },

  command1: function() { /* .. */ },
  command2: function() { /* .. */ },
  request1: function() { /* .. */ },
  request2: function() { /* .. */ }

});

And, now, commands/requests on the map channel will get routed accordingly:

var mapChannel = Radio.channel('map');
mapChannel.command('command1'); // Calls Map.command1();
mapChannel.request('request2'); // Calls Map.request2();

Bugs/Ideas/PRs

File an issue here, or hit me up on Twitter (@clured).