user1184100
user1184100

Reputation: 6894

How to pass a parameter to a function present in another view using EventAggregator - backbone

I saw the post access function in one view from another in backbone.js. If i want to pass parameter to loadTaskPopup(param) how can i do that ?

Backbone.View.prototype.event_aggregator = _.extend({}, Backbone.Events);

 window.PopupView = Backbone.View.extend({
   initialize: function() {
      _.bindAll(this, "loadTaskPopup");
      this.model = new PopupModel();
      this.event_aggregator.bind("tasks_popup:show", this.loadTaskPopup);
 },

 loadTaskPopup: function(param) {
    //do something with the parameter
 }
});

window.TaskbarView = Backbone.View.extend({
 loadTaskbarPopup: function() {
  this.event_aggregator.trigger("tasks_popup:show")     //How to pass parameter ?
 }
});

Upvotes: 0

Views: 976

Answers (1)

Cyclone
Cyclone

Reputation: 1580

You can pass it as a second parameter to the trigger:

this.event_aggregator.trigger("tasks_popup:show", {
  param1 : "value1",
  param2 : "value2"
});

In this case its an object. You'l receive this in loadTaskPopup.

loadTaskPopup : function(params) {
  // params.param1 and params.param2 will have value1 and value2 respectively here.
}

Upvotes: 2

Related Questions