Backbone.Wreqr vs Javascript Object

What are the main advantages of backbone.wreqr on js object both of which have access to the marionette event aggregator.
Would not assign / calling methods from an object in the same way as Commands / RequestResponse. For me, I do not see the need to implement this, other than providing a semantic / readability of +1.

https://github.com/marionettejs/backbone.wreqr
Can someone please enlighten me, this is my first basic (and modular) application.

+7
javascript marionette eventaggregator
source share
2 answers

Benefits:

  • events and command processing are optional, and you don’t need to manually check for undefined s
  • optionally several handlers for each event
  • lazy execution of commands (first a fire event, later register a command and it will be executed immediately)
  • you can define the scope without using any additional methods like $.proxy , ...
+12
source share

It provides an implementation of several common messaging patterns , including an Event Aggregation Pattern , Command Pattern , and Observer Pattern .

These patterns make it easy to untie implementations to reduce object dependencies. Consider a simple β€œCombat” game consisting of tanks and several targets. Without messaging templates, the tank must have explicit knowledge of the targets and how they work and cannot actually exist without the definition of target :

 var Tank = function(targets) { this.targets = targets }; Tank.prototype.fire = function() { var self = this, HpLoss = -500; _.each(this.targets, function(target) { if (self.isNear(target.coordinates) && target.canWithstand(HpLoss)) { target.die(); } } var target1 = new Target(coordinatesA, armorA); var target2 = new Target(coordinatesB, armorB); var tank = new Tank([target1, target2]); 

Using messaging templates such as Observer, tank in the code above does not need to know your goals; rather, the goals themselves can determine whether they should die:

 var Target = function() {} Target.prototype.calculateDamage = function(coordinates, damage) { if (this.isNear(coordinates) && !this.canWithstand(damage)) { this.die(); } } var Tank = function() {}; Tank.prototype.fire = function() { this.trigger('fire', { damage: 400, coordinates: this.location }); }; // Now Tank is entirely self-contained, and some external mediator can // make things happen at will: function main() { var target1 = new Target(coordinatesA, armorA); var target2 = new Target(coordinatesB, armorB); var tank = new Tank(); target1.listenTo(tank, 'fire', target1.calculateDamage, target1); target2.listenTo(tank, 'fire', target2.calculateDamage, target2); tank.fire(); var target3 = new Target3(coordinatesB, armorB); target3.listenTo(tank, 'fire', target3.calculateDamage, target3); } 
+8
source share

All Articles