0
votes

Say I have the following structure -

Model: Folder

Collection: Folders

From within my collection view, I want to trigger an event on a folder with a specific name -

So,

FolderCollectionView = Backbone.View.extend({
    ...
    ...
    editFolder: function() {
        this.collection.findWhere({ name: "abcd" }).trigger("editThisFolder");
    }
});

FolderModelView = Backbone.View.extend({
   ...
   ...
   editThisFolder: function() {
       //This should get called
   }
});

Is this possible? I'm using event aggregators, however, i haven't found a way wherein, I can trigger an event on a specific folder, I can make the folder view subscribe to a collection view event, but then all folder views respond to that event, I haven't found a way to make only a specific folder view respond to a collection event. Or somehow trigger an event on a specific folder view from the collection view.

I'm new to this, so let me know if i'm missing something important.

Event aggregator reference - http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

1
Yes you are on the right path using a custom eventjosephmisiti

1 Answers

1
votes

Backbone has underscore.js as a dependency. One of the reasons for this is the fact that Backbone.js Collections implement a whole lot of underscore methods. So your solution should work

this.collection.findWhere({ name: "abcd" }).trigger("editThisFolder");

Then of course you should make your FolderView listen to that event and call the function

var FolderView = Backbone.View.extend({
  initialize: function() {
    // assuming a folder model assigned to each view
    this.listenTo(this.model, 'editThisFolder', this.editThisFolder);
  }
});

Did this answer your question?