我想从视图#1调用一个已经在不同视图中实现的方法(视图#2)。 如何以一种简单的方式实现这一点..使用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){
},
});
答案 0 :(得分:1)
如果您搜索如何使用eventbus,您可以这样做:
// you can name the event 'custom_method_2' as you want
Backbone.Events.on('custom_method_2', App.Views.view2.custom_method_2);
现在,您正在收听对象custom_method_2
上的事件Backbone.Events
,您可以将其视为事件总线。
然后在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);
},