• This repository has been archived on 17/Nov/2020
  • Stars
    star
    346
  • Rank 122,430 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 12 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Messaging patterns for Backbone applications.

Backbone.Wreqr - DEPRECATED

A simple infrastructure based on messaging patterns and service bus implementations for decoupling Backbone and Backbone.Marionette applications.

Notice: In the next major release of Marionette, v3, Wreqr will be swapped for an updated library, Radio. If you've already begun using Wreqr, don't worry. This change isn't for quite some time: a few months, at the earliest. Also, we will support easily swapping the two libraries, so you won't run into any problems if you decide to continue using Wreqr.

For an introduction to Radio, check out our blog post. As of Marionette v2.1, you can easily swap in Radio for Wreqr with this shim. We think you'll really like the changes!

Downloads And Source

Grab the source from the src folder above. Grab the most recent builds from the links below.

Standard Builds

Basic Use

Event Aggregator

An event aggregator implementation. It extends from Backbone.Events to provide the core event handling code in an object that can itself be extended and instantiated as needed.

var vent = new Backbone.Wreqr.EventAggregator();

vent.on("foo", function(){
  console.log("foo event");
});

vent.trigger("foo");

Commands And Request / Response

Wreqr can be used by instantiating a Backbone.Wreqr.Commands or Backbone.Wreqr.RequestResponse object. These objects provide a setHandler method to add a handler for a named request or command. Commands can then be executed with the execute method, and request/response can be done through the request method.

Commands

var commands = new Backbone.Wreqr.Commands();

commands.setHandler("foo", function(){
  console.log("the foo command was executed");
});

commands.execute("foo");

Request/Response

var reqres = new Backbone.Wreqr.RequestResponse();

reqres.setHandler("foo", function(){
  return "foo requested. this is the response";
});

var result = reqres.request("foo");
console.log(result);

Radio

Radio is a convenient way for emitting events through channels. Radio can be used to either retrieve a channel, or talk through a channel with either command, reqres, or vent.

// channels
var globalChannel = Backbone.Wreqr.radio.channel('global');
var userChannel = Backbone.Wreqr.radio.channel('user');

// Wreqr events
Backbone.Wreqr.radio.commands.execute( 'global', 'shutdown' );
Backbone.Wreqr.radio.reqres.request(  'global', 'current-user' );
Backbone.Wreqr.radio.vent.trigger(  'global', 'game-over');

Channel

Channel is an object that wraps EventAggregator, Commands, and Reqres. Channels provide a convenient way for the objects in your system to talk to one another without the global channel becoming too noisy.

// global channel
var globalChannel = Backbone.Wreqr.radio.channel('global');
globalChannel.commands.execute('shutdown' );
globalChannel.reqres.request('current-user' );
globalChannel.vent.trigger('game-over');

// user channel
var userChannel = Backbone.Wreqr.radio.channel('user');
userChannel.commands.execute('punnish');
userChannel.reqres.request('user-avatar');
userChannel.vent.trigger('win', {
  level: 2,
  stars: 3
});

Adding Multiple Handlers

Multiple handlers can be set on the Commands and RequestResponse objects in a single call, using the setHandlers method and supplying a {"name": configuration} hash where the configuration is an object literal or a function.

var reqres = new Backbone.Wreqr.RequestResponse();

reqres.setHandlers({
  "foo": function(){ /* ... */ },
  "bar": {
    callback: function(){ /* ... */ },
    context: someObject
  }
});

var result = reqres.request("foo");

The "foo" handler is assigned directly to a function, while the "bar" handler is assigned to a function with a specific context to execute the function within.

This works for all Handlers, Commands and RequestResponse objects.

Removing Handlers

Removing handlers for commands or requests is done the same way, with the removeHandler or removeAllHandlers functions.

reqres.removeHandler("foo");

commands.removeAllHandlers();

Extending Wreqr Objects

The EventAggregator, Commands and RequestResponse objects can all be extended using Backbone's standard extend method.

var MyEventAgg = Backbone.Wreqr.EventAggregator.extend({
  foo: function(){...}
});

var MyCommands = Backbone.Wreqr.Commands.extend({
  foo: function(){...}
});

var MyReqRes = Backbone.Wreqr.RequestResponse.extend({
  foo: function(){...}
});

License

MIT - see LICENSE.md

Dev

  • npm install
  • npm install -g grunt-cli
  • grunt

More Repositories

1

backbone.marionette

The Backbone Framework
JavaScript
7,068
star
2

backbone.syphon

Serialize a Backbone.View in to a JavaScript object.
JavaScript
496
star
3

backbone.radio

Messaging patterns for Backbone applications.
JavaScript
490
star
4

backbone.babysitter

Manage child views for your Backbone.View (and other parents)
JavaScript
378
star
5

bbclonemail

[ Deprecated ] A Marionette.js reference application, cloning basic GMail features
JavaScript
219
star
6

marionette.inspector

🔍 Marionette Inspector - Explore your App
JavaScript
170
star
7

marionette-integrations

A collection of starter kits for building Marionette Applications
JavaScript
118
star
8

backbone.eventbinder

❌ [Deprecated] Manage Backbone Events Better
JavaScript
79
star
9

Marionette.Upgrade

Helps you upgrade your marionette apps from 1.x ➡️ 2.x
Python
42
star
10

backbone-metal

Classes, Mixins, Errors, and more.
JavaScript
31
star
11

guides

Learn how to build web apps using Marionette
27
star
12

backbone.marionette.async

❌ [Deprecated] Asynchronous template loading, rendering and more, for Backbone.Marionette
JavaScript
27
star
13

marionettejs.com

🚩 Source files for the Marionette site
Pug
26
star
14

marionette

JavaScript
22
star
15

marionette-v3-compat

Transitional monkey patch to make Marionette v3 compatible with v2.
JavaScript
8
star
16

www

❌ [deprecated] The old Marionette website
JavaScript
6
star
17

marionette.approuter

Extends the Backbone.Router to make it easier to construct a large number of routes for your app.
JavaScript
5
star
18

blog

📝 Blog for Marionettejs.com (hosted on the gh-pages branch)
CSS
4
star
19

marionette.oldcollectionview

Legacy Marionette v3 CollectionView and CompositeView
JavaScript
2
star
20

marionette.module

The (deprecated) Marionette.Module Class as a standalone library.
JavaScript
2
star
21

marionette.templatecache

Renderer which provides a cache for retrieving templates from script blocks in your HTML. This improved the speed of subsequent calls to get a template.
JavaScript
2
star
22

backbone-emulate-collection

Proxies underscore events to match a Backbone.Collection
JavaScript
1
star