I am reading the marionette.js docs and I don't understand the difference between vent
, reqres
and commands
.
The only thing I clearly understood is that commands should not return anything.
Can anyone explain it a little bit?
I am reading the marionette.js docs and I don't understand the difference between vent
, reqres
and commands
.
The only thing I clearly understood is that commands should not return anything.
Can anyone explain it a little bit?
Let's start from the top: Backbone.wreqr is a Backbone plugin that ships with Marionette. It provides three messaging patterns for loosely-coupled applications.
This answer includes example code from the Backbone.wreqr documentation - credit to the original authors.
EventAggregator
objects work like Backbone.Events
- they enable namespaced event handing. vent
is simply a common variable name for an EventAggregator
object:
var vent = new Backbone.Wreqr.EventAggregator();
vent.on("foo", function(){
console.log("foo event");
});
vent.trigger("foo");
Commands are very similar to Events. The difference is semantic - an event informs other parts of the application that something has happened. A command instructs another part of the application to do something.
var commands = new Backbone.Wreqr.Commands();
commands.setHandler("foo", function(){
console.log("the foo command was executed");
});
commands.execute("foo");
RequestResponse
objects, which are often referenced by a variable called reqres
, provide a loosely-coupled way for application components to request access to objects:
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);
As a convenience, Wreqr provides an object called radio
which mixes in the three messaging patterns. Commands, events and requests can be grouped into logical channels to prevent interference - you may need distinct save
commands for user
and document
channels, for example.
Marionette.Application
creates instances of Commands
, RequestResponse
and EventAggregator
inside a channel ("global" by default)
using the conventional variable names. If you need custom behaviour you can override the vent
, commands
and reqres
variables.
_initChannel: function() {
this.channelName = _.result(this, 'channelName') || 'global';
this.channel = _.result(this, 'channel') || Backbone.Wreqr.radio.channel(this.channelName);
this.vent = _.result(this, 'vent') || this.channel.vent;
this.commands = _.result(this, 'commands') || this.channel.commands;
this.reqres = _.result(this, 'reqres') || this.channel.reqres;
},
I suggest you read the Wreqr docs for more detail. I also recommend reading through the Marionette annotated source - it is concise and very well documented and, in fact, includes the Wreqr source.
N.B. The next major release of Marionnette, v3.x, replaces Wreqr with Radio. Radio provides the same functionality as Wreqr with a cleaner API. It is possible to use Radio in Marionette 2.x applications, which I recommend if you are starting a new app.
ReqRes
MessengerThe reqres
messenger can both send a message, (which consists of the named event
as well as optional parameters), to a target and relay a response back to the source (which will be in the form of the returned parameter of the target function)
Command
MessengerThe command
and vent
messengers are functionally very similar, but fulfill different semantic responsibilities.
The command
messenger is used to invoke the execution of a target function. Generally, one function is bound to a command
handler. Like the OP stated, in a command
the direction of communication is one-way, meaning that whatever the command
target returns will not be sent back to the source incoming the command
.
VENT
MessengerFinally, vent
messengers are event aggregators. You can attach (subscribe) many listeners to the vent
and all of them will receive an event that is triggered (published) by the vent
. Each listener will invoke a function associated with that listener When an event in a ven
t is triggered it may also send each listener a set of parameters.
© 2022 - 2024 — McMap. All rights reserved.
Radio
upgrade in Marionette v3.0 – Deka