最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a function in a backbone view from another backbone view - Stack Overflow

programmeradmin2浏览0评论

In my current application, I have a save function in one of my backbone views.

custom_save : function() { // this method's save code // have to call another views save function here }

The other view name is App.SettingsView and it has a save method. I have to call this save method right after the custom_save logic. How can I call App.SettingsView save function inside custom_save function. Please not both are 2 different files

Thanks

In my current application, I have a save function in one of my backbone views.

custom_save : function() { // this method's save code // have to call another views save function here }

The other view name is App.SettingsView and it has a save method. I have to call this save method right after the custom_save logic. How can I call App.SettingsView save function inside custom_save function. Please not both are 2 different files

Thanks

Share Improve this question asked Dec 24, 2012 at 5:12 MujahidMujahid 1,2375 gold badges32 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

It's a bad practice you got there. Coupling between the views. Why don't you create a EventBus that inherits from the Backbone.Events then trigger the event that the another view subscribes too. When event occurs just trigger the save function that's it

View#1 Save

save:function(){
 EventBus.trigger("save:view");
}

View#2 Save

initialize:function(){
 EventBus.on("save:view:",this.save);
},
save:function(){
//your code
}

Sounds nice ? It should :)

Create a new view object and call it.

var anotherView = new App.SettingView();
anotherView.save();
发布评论

评论列表(0)

  1. 暂无评论