Ashish Sajwan
Ashish Sajwan

Reputation: 704

how to use method implemented in a view from another view : backbonejs

i want to call a method from view#1 which is already implemented in different view (view#2).. how to achieve this in a nice n simple way.. using backbonejs.

App.Views.view1 = Backbone.View.extend({
   events: {
       'click .someclass1' : 'custom_method_1',
         },

        custom_method_1:function(e){

         //now this method calls another method which is implemented in different view
         custom_method_2();
    },
});


 App.Views.view2 = Backbone.View.extend({
   events: {
       'click .someclass2' : 'custom_method_2',
         },

   //// this method needs to be called from view1 also
        custom_method_2:function(e){

    },
});

Upvotes: 0

Views: 57

Answers (1)

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

If you search how to use the eventbus, you can do it like this:

// you can name the event 'custom_method_2' as you want
Backbone.Events.on('custom_method_2', App.Views.view2.custom_method_2);

Now you are listening to the event custom_method_2 on the Object Backbone.Events that you can consider as your eventsbus.

Then in view1:

custom_method_1:function(e){

     //now this method calls another method which is implemented in different view
     // custom_method_2();
     Backbone.Events.trigger('custom_method_2', e);
},

Upvotes: 1

Related Questions