1
0
mirror of https://github.com/2ec0b4/kaamelott-soundboard.git synced 2025-12-10 16:35:34 +00:00

(master) Ajoute les dépendances manquantes

This commit is contained in:
Antoine
2023-10-30 08:04:13 +01:00
parent dca241fc0b
commit 8930cd2dc7
268 changed files with 83121 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
{
"name": "backbone.wreqr",
"version": "1.4.0",
"homepage": "https://github.com/marionettejs/backbone.wreqr",
"authors": [
"Derick Bailey"
],
"description": "A Simple Service Bus For Backbone and Backbone.Marionette",
"main": "lib/backbone.wreqr.js",
"keywords": [
"backbone",
"plugin",
"computed",
"field",
"model"
],
"dependencies": {
"backbone": ">=0.9.9 <=1.3.x",
"underscore": ">=1.3.3 <=1.8.3"
},
"devDependencies": {
"grunt": "0.4.0",
"grunt-contrib-jasmine": "0.4.1",
"grunt-contrib-uglify": "0.1.2",
"grunt-contrib-concat": "0.1.3",
"grunt-contrib-jshint": "0.2.0",
"grunt-contrib-watch": "0.3.1",
"grunt-contrib-connect": "0.2.0",
"grunt-template-jasmine-istanbul": "0.2.0",
"grunt-preprocess": "2.0.0",
"grunt-plato": "0.1.5"
},
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests",
"public",
"spec",
".gitignore",
".jshintrc",
".travis.yml",
"Gruntfile.js",
"component.json",
"package.json"
],
"_release": "1.4.0",
"_resolution": {
"type": "version",
"tag": "v1.4.0",
"commit": "ce4665f7e13ec46d66fe9215f5a91199e29dff51"
},
"_source": "https://github.com/marionettejs/backbone.wreqr.git",
"_target": "^1.0.0",
"_originalSource": "backbone.wreqr"
}

View File

@@ -0,0 +1,84 @@
# Change log
### v1.4.0
* Align code to be able to work with lodash 4.x in addition to underscore
### v1.3.7
* Bump version range of backbone.
### v1.3.6
* Bump version range of backbone.
### v1.3.5
* Bump version range of backbone.
### v1.3.4
* Bump version range of backbone.
### v1.3.3
* Bump version range of backbone and underscore support.
### v1.3.2
* Increase underscore range
* Minor underscore refactors
### v1.3.1
* Fix UMD setup and build process
### v1.3.0
* Add Wreqr.noConflict()
* Add Wreqr.VERSION
* Fixed a bug where reqres would not return a value using the 'top-level' Channel API.
### v1.2.1
* Remove AMD builds and add a single UMD style build.
### v1.2.0
* Adds Radio, allowing you to create explicit namespaces called Channels. A Channel is made up of
an instance of each of the three messaging systems.
### v1.1.0
* Removes the Error on unhandled commands/requests
### v1.0.1
* update dependencies
### v1.0.0
* major version release
* minor fixes
### v0.2.0
* Handlers (Commands/RequestResponse)
* **BREAKING:** renamed `addHandler` to `setHandler` to clarify the point of single handlers per named item
* Allow an `initialize` function when extending from the base type
* Added a `setHandlers` function that takes an object literal as the parameter, to configure multiple handlers in a single call
* Commands
* Introduced Wreqr.CommandStorage to store commands for later execution
* When a command has no handler, it will be stored for later execution
* When a handler for a stored command is added, the stored command will be
executed
* Updated build process to use Grunt v0.4
### v0.1.1
* Fixed "option strict" to be "use strict" ... #facepalm :P
* Added jam package config
### v0.1.0
* Fix calls to `.apply` to account for IE < 9 throwing an error when `arguments` is null or undefined
### v0.0.1
* Commands
* Can specify multiple arguments for `execute` method
* RequestResponse
* Can specify multiple arguments for `request` method
### v0.0.0
* Initial release

View File

@@ -0,0 +1,5 @@
# Backbone.Wreqr
Copyright (C)2012 Derick Bailey, Muted Solutions, LLC
Distributed Under MIT License

View File

@@ -0,0 +1,49 @@
{
"name": "backbone.wreqr",
"version": "1.4.0",
"homepage": "https://github.com/marionettejs/backbone.wreqr",
"authors": [
"Derick Bailey"
],
"description": "A Simple Service Bus For Backbone and Backbone.Marionette",
"main": "lib/backbone.wreqr.js",
"keywords": [
"backbone",
"plugin",
"computed",
"field",
"model"
],
"dependencies": {
"backbone": ">=0.9.9 <=1.3.x",
"underscore": ">=1.3.3 <=1.8.3"
},
"devDependencies": {
"grunt": "0.4.0",
"grunt-contrib-jasmine": "0.4.1",
"grunt-contrib-uglify": "0.1.2",
"grunt-contrib-concat": "0.1.3",
"grunt-contrib-jshint": "0.2.0",
"grunt-contrib-watch": "0.3.1",
"grunt-contrib-connect": "0.2.0",
"grunt-template-jasmine-istanbul": "0.2.0",
"grunt-preprocess": "2.0.0",
"grunt-plato": "0.1.5"
},
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests",
"public",
"spec",
".gitignore",
".jshintrc",
".travis.yml",
"Gruntfile.js",
"component.json",
"package.json"
]
}

View File

@@ -0,0 +1,435 @@
// Backbone.Wreqr (Backbone.Marionette)
// ----------------------------------
// v1.4.0
//
// Copyright (c)2016 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
//
// http://github.com/marionettejs/backbone.wreqr
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], function(Backbone, _) {
return factory(Backbone, _);
});
} else if (typeof exports !== 'undefined') {
var Backbone = require('backbone');
var _ = require('underscore');
module.exports = factory(Backbone, _);
} else {
factory(root.Backbone, root._);
}
}(this, function(Backbone, _) {
"use strict";
var previousWreqr = Backbone.Wreqr;
var Wreqr = Backbone.Wreqr = {};
Backbone.Wreqr.VERSION = '1.4.0';
Backbone.Wreqr.noConflict = function () {
Backbone.Wreqr = previousWreqr;
return this;
};
// Handlers
// --------
// A registry of functions to call, given a name
Wreqr.Handlers = (function(Backbone, _){
"use strict";
// Constructor
// -----------
var Handlers = function(options){
this.options = options;
this._wreqrHandlers = {};
if (_.isFunction(this.initialize)){
this.initialize(options);
}
};
Handlers.extend = Backbone.Model.extend;
// Instance Members
// ----------------
_.extend(Handlers.prototype, Backbone.Events, {
// Add multiple handlers using an object literal configuration
setHandlers: function(handlers){
_.each(handlers, _.bind(function(handler, name){
var context = null;
if (_.isObject(handler) && !_.isFunction(handler)){
context = handler.context;
handler = handler.callback;
}
this.setHandler(name, handler, context);
}, this));
},
// Add a handler for the given name, with an
// optional context to run the handler within
setHandler: function(name, handler, context){
var config = {
callback: handler,
context: context
};
this._wreqrHandlers[name] = config;
this.trigger("handler:add", name, handler, context);
},
// Determine whether or not a handler is registered
hasHandler: function(name){
return !! this._wreqrHandlers[name];
},
// Get the currently registered handler for
// the specified name. Throws an exception if
// no handler is found.
getHandler: function(name){
var config = this._wreqrHandlers[name];
if (!config){
return;
}
return function(){
return config.callback.apply(config.context, arguments);
};
},
// Remove a handler for the specified name
removeHandler: function(name){
delete this._wreqrHandlers[name];
},
// Remove all handlers from this registry
removeAllHandlers: function(){
this._wreqrHandlers = {};
}
});
return Handlers;
})(Backbone, _);
// Wreqr.CommandStorage
// --------------------
//
// Store and retrieve commands for execution.
Wreqr.CommandStorage = (function(){
"use strict";
// Constructor function
var CommandStorage = function(options){
this.options = options;
this._commands = {};
if (_.isFunction(this.initialize)){
this.initialize(options);
}
};
// Instance methods
_.extend(CommandStorage.prototype, Backbone.Events, {
// Get an object literal by command name, that contains
// the `commandName` and the `instances` of all commands
// represented as an array of arguments to process
getCommands: function(commandName){
var commands = this._commands[commandName];
// we don't have it, so add it
if (!commands){
// build the configuration
commands = {
command: commandName,
instances: []
};
// store it
this._commands[commandName] = commands;
}
return commands;
},
// Add a command by name, to the storage and store the
// args for the command
addCommand: function(commandName, args){
var command = this.getCommands(commandName);
command.instances.push(args);
},
// Clear all commands for the given `commandName`
clearCommands: function(commandName){
var command = this.getCommands(commandName);
command.instances = [];
}
});
return CommandStorage;
})();
// Wreqr.Commands
// --------------
//
// A simple command pattern implementation. Register a command
// handler and execute it.
Wreqr.Commands = (function(Wreqr, _){
"use strict";
return Wreqr.Handlers.extend({
// default storage type
storageType: Wreqr.CommandStorage,
constructor: function(options){
this.options = options || {};
this._initializeStorage(this.options);
this.on("handler:add", this._executeCommands, this);
Wreqr.Handlers.prototype.constructor.apply(this, arguments);
},
// Execute a named command with the supplied args
execute: function(name){
name = arguments[0];
var args = _.rest(arguments);
if (this.hasHandler(name)){
this.getHandler(name).apply(this, args);
} else {
this.storage.addCommand(name, args);
}
},
// Internal method to handle bulk execution of stored commands
_executeCommands: function(name, handler, context){
var command = this.storage.getCommands(name);
// loop through and execute all the stored command instances
_.each(command.instances, function(args){
handler.apply(context, args);
});
this.storage.clearCommands(name);
},
// Internal method to initialize storage either from the type's
// `storageType` or the instance `options.storageType`.
_initializeStorage: function(options){
var storage;
var StorageType = options.storageType || this.storageType;
if (_.isFunction(StorageType)){
storage = new StorageType();
} else {
storage = StorageType;
}
this.storage = storage;
}
});
})(Wreqr, _);
// Wreqr.RequestResponse
// ---------------------
//
// A simple request/response implementation. Register a
// request handler, and return a response from it
Wreqr.RequestResponse = (function(Wreqr, _){
"use strict";
return Wreqr.Handlers.extend({
request: function(name){
if (this.hasHandler(name)) {
return this.getHandler(name).apply(this, _.rest(arguments));
}
}
});
})(Wreqr, _);
// Event Aggregator
// ----------------
// A pub-sub object that can be used to decouple various parts
// of an application through event-driven architecture.
Wreqr.EventAggregator = (function(Backbone, _){
"use strict";
var EA = function(){};
// Copy the `extend` function used by Backbone's classes
EA.extend = Backbone.Model.extend;
// Copy the basic Backbone.Events on to the event aggregator
_.extend(EA.prototype, Backbone.Events);
return EA;
})(Backbone, _);
// Wreqr.Channel
// --------------
//
// An object that wraps the three messaging systems:
// EventAggregator, RequestResponse, Commands
Wreqr.Channel = (function(Wreqr){
"use strict";
var Channel = function(channelName) {
this.vent = new Backbone.Wreqr.EventAggregator();
this.reqres = new Backbone.Wreqr.RequestResponse();
this.commands = new Backbone.Wreqr.Commands();
this.channelName = channelName;
};
_.extend(Channel.prototype, {
// Remove all handlers from the messaging systems of this channel
reset: function() {
this.vent.off();
this.vent.stopListening();
this.reqres.removeAllHandlers();
this.commands.removeAllHandlers();
return this;
},
// Connect a hash of events; one for each messaging system
connectEvents: function(hash, context) {
this._connect('vent', hash, context);
return this;
},
connectCommands: function(hash, context) {
this._connect('commands', hash, context);
return this;
},
connectRequests: function(hash, context) {
this._connect('reqres', hash, context);
return this;
},
// Attach the handlers to a given message system `type`
_connect: function(type, hash, context) {
if (!hash) {
return;
}
context = context || this;
var method = (type === 'vent') ? 'on' : 'setHandler';
_.each(hash, _.bind(function(fn, eventName) {
this[type][method](eventName, _.bind(fn, context));
}, this));
}
});
return Channel;
})(Wreqr);
// Wreqr.Radio
// --------------
//
// An object that lets you communicate with many channels.
Wreqr.radio = (function(Wreqr, _){
"use strict";
var Radio = function() {
this._channels = {};
this.vent = {};
this.commands = {};
this.reqres = {};
this._proxyMethods();
};
_.extend(Radio.prototype, {
channel: function(channelName) {
if (!channelName) {
throw new Error('Channel must receive a name');
}
return this._getChannel( channelName );
},
_getChannel: function(channelName) {
var channel = this._channels[channelName];
if(!channel) {
channel = new Wreqr.Channel(channelName);
this._channels[channelName] = channel;
}
return channel;
},
_proxyMethods: function() {
_.each(['vent', 'commands', 'reqres'], _.bind(function(system) {
_.each( messageSystems[system], _.bind(function(method) {
this[system][method] = proxyMethod(this, system, method);
}, this));
}, this));
}
});
var messageSystems = {
vent: [
'on',
'off',
'trigger',
'once',
'stopListening',
'listenTo',
'listenToOnce'
],
commands: [
'execute',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
],
reqres: [
'request',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
]
};
var proxyMethod = function(radio, system, method) {
return function(channelName) {
var messageSystem = radio._getChannel(channelName)[system];
return messageSystem[method].apply(messageSystem, _.rest(arguments));
};
};
return new Radio();
})(Wreqr, _);
return Backbone.Wreqr;
}));

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,13 @@
// Backbone.Wreqr (Backbone.Marionette)
// ----------------------------------
// v1.4.0
//
// Copyright (c)2016 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
//
// http://github.com/marionettejs/backbone.wreqr
!function(a,b){if("function"==typeof define&&define.amd)define(["backbone","underscore"],function(a,c){return b(a,c)});else if("undefined"!=typeof exports){var c=require("backbone"),d=require("underscore");module.exports=b(c,d)}else b(a.Backbone,a._)}(this,function(a,b){"use strict";var c=a.Wreqr,d=a.Wreqr={};return a.Wreqr.VERSION="1.4.0",a.Wreqr.noConflict=function(){return a.Wreqr=c,this},d.Handlers=function(a,b){var c=function(a){this.options=a,this._wreqrHandlers={},b.isFunction(this.initialize)&&this.initialize(a)};return c.extend=a.Model.extend,b.extend(c.prototype,a.Events,{setHandlers:function(a){b.each(a,b.bind(function(a,c){var d=null;b.isObject(a)&&!b.isFunction(a)&&(d=a.context,a=a.callback),this.setHandler(c,a,d)},this))},setHandler:function(a,b,c){var d={callback:b,context:c};this._wreqrHandlers[a]=d,this.trigger("handler:add",a,b,c)},hasHandler:function(a){return!!this._wreqrHandlers[a]},getHandler:function(a){var b=this._wreqrHandlers[a];if(b)return function(){return b.callback.apply(b.context,arguments)}},removeHandler:function(a){delete this._wreqrHandlers[a]},removeAllHandlers:function(){this._wreqrHandlers={}}}),c}(a,b),d.CommandStorage=function(){var c=function(a){this.options=a,this._commands={},b.isFunction(this.initialize)&&this.initialize(a)};return b.extend(c.prototype,a.Events,{getCommands:function(a){var b=this._commands[a];return b||(b={command:a,instances:[]},this._commands[a]=b),b},addCommand:function(a,b){var c=this.getCommands(a);c.instances.push(b)},clearCommands:function(a){var b=this.getCommands(a);b.instances=[]}}),c}(),d.Commands=function(a,b){return a.Handlers.extend({storageType:a.CommandStorage,constructor:function(b){this.options=b||{},this._initializeStorage(this.options),this.on("handler:add",this._executeCommands,this),a.Handlers.prototype.constructor.apply(this,arguments)},execute:function(a){a=arguments[0];var c=b.rest(arguments);this.hasHandler(a)?this.getHandler(a).apply(this,c):this.storage.addCommand(a,c)},_executeCommands:function(a,c,d){var e=this.storage.getCommands(a);b.each(e.instances,function(a){c.apply(d,a)}),this.storage.clearCommands(a)},_initializeStorage:function(a){var c,d=a.storageType||this.storageType;c=b.isFunction(d)?new d:d,this.storage=c}})}(d,b),d.RequestResponse=function(a,b){return a.Handlers.extend({request:function(a){return this.hasHandler(a)?this.getHandler(a).apply(this,b.rest(arguments)):void 0}})}(d,b),d.EventAggregator=function(a,b){var c=function(){};return c.extend=a.Model.extend,b.extend(c.prototype,a.Events),c}(a,b),d.Channel=function(c){var d=function(b){this.vent=new a.Wreqr.EventAggregator,this.reqres=new a.Wreqr.RequestResponse,this.commands=new a.Wreqr.Commands,this.channelName=b};return b.extend(d.prototype,{reset:function(){return this.vent.off(),this.vent.stopListening(),this.reqres.removeAllHandlers(),this.commands.removeAllHandlers(),this},connectEvents:function(a,b){return this._connect("vent",a,b),this},connectCommands:function(a,b){return this._connect("commands",a,b),this},connectRequests:function(a,b){return this._connect("reqres",a,b),this},_connect:function(a,c,d){if(c){d=d||this;var e="vent"===a?"on":"setHandler";b.each(c,b.bind(function(c,f){this[a][e](f,b.bind(c,d))},this))}}}),d}(d),d.radio=function(a,b){var c=function(){this._channels={},this.vent={},this.commands={},this.reqres={},this._proxyMethods()};b.extend(c.prototype,{channel:function(a){if(!a)throw new Error("Channel must receive a name");return this._getChannel(a)},_getChannel:function(b){var c=this._channels[b];return c||(c=new a.Channel(b),this._channels[b]=c),c},_proxyMethods:function(){b.each(["vent","commands","reqres"],b.bind(function(a){b.each(d[a],b.bind(function(b){this[a][b]=e(this,a,b)},this))},this))}});var d={vent:["on","off","trigger","once","stopListening","listenTo","listenToOnce"],commands:["execute","setHandler","setHandlers","removeHandler","removeAllHandlers"],reqres:["request","setHandler","setHandlers","removeHandler","removeAllHandlers"]},e=function(a,c,d){return function(e){var f=a._getChannel(e)[c];return f[d].apply(f,b.rest(arguments))}};return new c}(d,b),a.Wreqr});
//# sourceMappingURL=backbone.wreqr.min.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,186 @@
<h1 align="center">Backbone.Wreqr</h1>
<p align="center">
<a title='Build Status' href="https://travis-ci.org/marionettejs/backbone.wreqr">
<img src='https://travis-ci.org/marionettejs/backbone.wreqr.svg' />
</a>
</p>
A simple infrastructure based on [messaging patterns](http://www.eaipatterns.com/)
and service bus implementations for decoupling [Backbone](http://backbonejs.org)
and [Backbone.Marionette](http://marionettejs.com) applications.
**Notice:** In the next major release of Marionette, v3, Wreqr will be swapped for an updated library,
[Radio](https://github.com/jmeas/backbone.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](http://blog.marionettejs.com/2014/07/11/introducing-backbone-radio/index.html). As of Marionette v2.1, you can easily
swap in Radio for Wreqr with [this shim](https://gist.github.com/jmeas/7992474cdb1c5672d88b). 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
* Development: [backbone.wreqr.js](https://raw.github.com/marionettejs/backbone.wreqr/master/lib/backbone.wreqr.js)
* Production: [backbone.wreqr.min.js](https://raw.github.com/marionettejs/backbone.wreqr/master/lib/backbone.wreqr.min.js)
## 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.
```js
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
```js
var commands = new Backbone.Wreqr.Commands();
commands.setHandler("foo", function(){
console.log("the foo command was executed");
});
commands.execute("foo");
```
### Request/Response
```js
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.
```js
// 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.
```js
// 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.
```js
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.
```js
reqres.removeHandler("foo");
commands.removeAllHandlers();
```
### Extending Wreqr Objects
The EventAggregator, Commands and RequestResponse objects can all be
extended using Backbone's standard `extend` method.
```js
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](https://raw.github.com/marionettejs/backbone.wreqr/master/LICENSE.md)
## Dev
* `npm install`
* `npm install -g grunt-cli`
* `grunt`

View File

@@ -0,0 +1,39 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], function(Backbone, _) {
return factory(Backbone, _);
});
} else if (typeof exports !== 'undefined') {
var Backbone = require('backbone');
var _ = require('underscore');
module.exports = factory(Backbone, _);
} else {
factory(root.Backbone, root._);
}
}(this, function(Backbone, _) {
"use strict";
var previousWreqr = Backbone.Wreqr;
var Wreqr = Backbone.Wreqr = {};
Backbone.Wreqr.VERSION = '<%= version %>';
Backbone.Wreqr.noConflict = function () {
Backbone.Wreqr = previousWreqr;
return this;
};
// @include ../wreqr.handlers.js
// @include ../wreqr.commandStorage.js
// @include ../wreqr.commands.js
// @include ../wreqr.requestresponse.js
// @include ../wreqr.eventaggregator.js
// @include ../wreqr.channel.js
// @include ../wreqr.radio.js
return Backbone.Wreqr;
}));

View File

@@ -0,0 +1,60 @@
// Wreqr.Channel
// --------------
//
// An object that wraps the three messaging systems:
// EventAggregator, RequestResponse, Commands
Wreqr.Channel = (function(Wreqr){
"use strict";
var Channel = function(channelName) {
this.vent = new Backbone.Wreqr.EventAggregator();
this.reqres = new Backbone.Wreqr.RequestResponse();
this.commands = new Backbone.Wreqr.Commands();
this.channelName = channelName;
};
_.extend(Channel.prototype, {
// Remove all handlers from the messaging systems of this channel
reset: function() {
this.vent.off();
this.vent.stopListening();
this.reqres.removeAllHandlers();
this.commands.removeAllHandlers();
return this;
},
// Connect a hash of events; one for each messaging system
connectEvents: function(hash, context) {
this._connect('vent', hash, context);
return this;
},
connectCommands: function(hash, context) {
this._connect('commands', hash, context);
return this;
},
connectRequests: function(hash, context) {
this._connect('reqres', hash, context);
return this;
},
// Attach the handlers to a given message system `type`
_connect: function(type, hash, context) {
if (!hash) {
return;
}
context = context || this;
var method = (type === 'vent') ? 'on' : 'setHandler';
_.each(hash, _.bind(function(fn, eventName) {
this[type][method](eventName, _.bind(fn, context));
}, this));
}
});
return Channel;
})(Wreqr);

View File

@@ -0,0 +1,58 @@
// Wreqr.CommandStorage
// --------------------
//
// Store and retrieve commands for execution.
Wreqr.CommandStorage = (function(){
"use strict";
// Constructor function
var CommandStorage = function(options){
this.options = options;
this._commands = {};
if (_.isFunction(this.initialize)){
this.initialize(options);
}
};
// Instance methods
_.extend(CommandStorage.prototype, Backbone.Events, {
// Get an object literal by command name, that contains
// the `commandName` and the `instances` of all commands
// represented as an array of arguments to process
getCommands: function(commandName){
var commands = this._commands[commandName];
// we don't have it, so add it
if (!commands){
// build the configuration
commands = {
command: commandName,
instances: []
};
// store it
this._commands[commandName] = commands;
}
return commands;
},
// Add a command by name, to the storage and store the
// args for the command
addCommand: function(commandName, args){
var command = this.getCommands(commandName);
command.instances.push(args);
},
// Clear all commands for the given `commandName`
clearCommands: function(commandName){
var command = this.getCommands(commandName);
command.instances = [];
}
});
return CommandStorage;
})();

View File

@@ -0,0 +1,63 @@
// Wreqr.Commands
// --------------
//
// A simple command pattern implementation. Register a command
// handler and execute it.
Wreqr.Commands = (function(Wreqr, _){
"use strict";
return Wreqr.Handlers.extend({
// default storage type
storageType: Wreqr.CommandStorage,
constructor: function(options){
this.options = options || {};
this._initializeStorage(this.options);
this.on("handler:add", this._executeCommands, this);
Wreqr.Handlers.prototype.constructor.apply(this, arguments);
},
// Execute a named command with the supplied args
execute: function(name){
name = arguments[0];
var args = _.rest(arguments);
if (this.hasHandler(name)){
this.getHandler(name).apply(this, args);
} else {
this.storage.addCommand(name, args);
}
},
// Internal method to handle bulk execution of stored commands
_executeCommands: function(name, handler, context){
var command = this.storage.getCommands(name);
// loop through and execute all the stored command instances
_.each(command.instances, function(args){
handler.apply(context, args);
});
this.storage.clearCommands(name);
},
// Internal method to initialize storage either from the type's
// `storageType` or the instance `options.storageType`.
_initializeStorage: function(options){
var storage;
var StorageType = options.storageType || this.storageType;
if (_.isFunction(StorageType)){
storage = new StorageType();
} else {
storage = StorageType;
}
this.storage = storage;
}
});
})(Wreqr, _);

View File

@@ -0,0 +1,17 @@
// Event Aggregator
// ----------------
// A pub-sub object that can be used to decouple various parts
// of an application through event-driven architecture.
Wreqr.EventAggregator = (function(Backbone, _){
"use strict";
var EA = function(){};
// Copy the `extend` function used by Backbone's classes
EA.extend = Backbone.Model.extend;
// Copy the basic Backbone.Events on to the event aggregator
_.extend(EA.prototype, Backbone.Events);
return EA;
})(Backbone, _);

View File

@@ -0,0 +1,86 @@
// Handlers
// --------
// A registry of functions to call, given a name
Wreqr.Handlers = (function(Backbone, _){
"use strict";
// Constructor
// -----------
var Handlers = function(options){
this.options = options;
this._wreqrHandlers = {};
if (_.isFunction(this.initialize)){
this.initialize(options);
}
};
Handlers.extend = Backbone.Model.extend;
// Instance Members
// ----------------
_.extend(Handlers.prototype, Backbone.Events, {
// Add multiple handlers using an object literal configuration
setHandlers: function(handlers){
_.each(handlers, _.bind(function(handler, name){
var context = null;
if (_.isObject(handler) && !_.isFunction(handler)){
context = handler.context;
handler = handler.callback;
}
this.setHandler(name, handler, context);
}, this));
},
// Add a handler for the given name, with an
// optional context to run the handler within
setHandler: function(name, handler, context){
var config = {
callback: handler,
context: context
};
this._wreqrHandlers[name] = config;
this.trigger("handler:add", name, handler, context);
},
// Determine whether or not a handler is registered
hasHandler: function(name){
return !! this._wreqrHandlers[name];
},
// Get the currently registered handler for
// the specified name. Throws an exception if
// no handler is found.
getHandler: function(name){
var config = this._wreqrHandlers[name];
if (!config){
return;
}
return function(){
return config.callback.apply(config.context, arguments);
};
},
// Remove a handler for the specified name
removeHandler: function(name){
delete this._wreqrHandlers[name];
},
// Remove all handlers from this registry
removeAllHandlers: function(){
this._wreqrHandlers = {};
}
});
return Handlers;
})(Backbone, _);

View File

@@ -0,0 +1,85 @@
// Wreqr.Radio
// --------------
//
// An object that lets you communicate with many channels.
Wreqr.radio = (function(Wreqr, _){
"use strict";
var Radio = function() {
this._channels = {};
this.vent = {};
this.commands = {};
this.reqres = {};
this._proxyMethods();
};
_.extend(Radio.prototype, {
channel: function(channelName) {
if (!channelName) {
throw new Error('Channel must receive a name');
}
return this._getChannel( channelName );
},
_getChannel: function(channelName) {
var channel = this._channels[channelName];
if(!channel) {
channel = new Wreqr.Channel(channelName);
this._channels[channelName] = channel;
}
return channel;
},
_proxyMethods: function() {
_.each(['vent', 'commands', 'reqres'], _.bind(function(system) {
_.each( messageSystems[system], _.bind(function(method) {
this[system][method] = proxyMethod(this, system, method);
}, this));
}, this));
}
});
var messageSystems = {
vent: [
'on',
'off',
'trigger',
'once',
'stopListening',
'listenTo',
'listenToOnce'
],
commands: [
'execute',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
],
reqres: [
'request',
'setHandler',
'setHandlers',
'removeHandler',
'removeAllHandlers'
]
};
var proxyMethod = function(radio, system, method) {
return function(channelName) {
var messageSystem = radio._getChannel(channelName)[system];
return messageSystem[method].apply(messageSystem, _.rest(arguments));
};
};
return new Radio();
})(Wreqr, _);

View File

@@ -0,0 +1,17 @@
// Wreqr.RequestResponse
// ---------------------
//
// A simple request/response implementation. Register a
// request handler, and return a response from it
Wreqr.RequestResponse = (function(Wreqr, _){
"use strict";
return Wreqr.Handlers.extend({
request: function(name){
if (this.hasHandler(name)) {
return this.getHandler(name).apply(this, _.rest(arguments));
}
}
});
})(Wreqr, _);