Event Pass Object

Reading backbone lessons, it seems that when an event is addfired from collection, the added element is sent along with the event (the same thing happens for remove). I can not find the documentation for this feature on the backbonejs.org website , and I was curious if there was a way to send the object along with my custom one events. Secondly, is something like this possible at the Marionet?

+3
source share
2 answers

Each object specified by Backbone is mixed in Backbone.Events, which means you can fire events with object.trigger. It is defined as

trigger object.trigger(event, [* args])
, . .

, .

,

var m = new Backbone.Model();
m.on('custom', function(more) {
     console.log(more);
});
m.trigger('custom', 'more info');

more info

http://jsfiddle.net/nikoshr/HpwXe/

, :

var m = new Backbone.Model();
m.on('custom', function(model, more) {
     console.log(arguments);
});
m.trigger('custom', m, 'more info');

http://jsfiddle.net/nikoshr/HpwXe/1/

:

var M = Backbone.Model.extend({
    custom: function() {
        this.trigger('custom', this);
    }
});

var m = new M();
m.on('custom', function(model, more) {
     console.log(model);
});
m.custom();

http://jsfiddle.net/nikoshr/HpwXe/2/

+4

, , Backbone.Event

var collection = Backbone.Collection.extend();
collection = new collection();
collection.on("message", function(message){
  console.log(message);
});

var model = new Backbone.Model();
collection.add(model);
model.trigger("message", "This is message");

, .

Event Aggregato r Marionette.js

. Backbone.Events , .

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

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

vent.trigger("foo");
0

All Articles