主干:如何引发父视图的方法 [英] Backbone: How to trigger methods in a parent view
问题描述
我有一个字母过滤视图(A,B,C,D等),很多观点将被使用。我已经安装在主视图的方法来获取使用点击信中的API的结果。
I have an alphabet filter view (a, b, c, d etc) that a lot of views will be using. I have setup a method on the main view to fetch results from the API using the letter clicked.
我通过下调传递一个回调函数字母表过滤器按下面已经设置这样的:
I've setup this by passing a callback function down to the alphabet filter as per below:
view = new App.Views.Common.AlphabetFiltersIndexView(filterCallback: @paginationFilter)
@$(".pagination-vertical").replaceWith(view.render().el)
调用filterCallback和传递参数的作品,但是现在叫paginationFilter方法属于字母的过滤器。
Calling the filterCallback and passing arguments works, however the paginationFilter method called now belongs to the Alphabet Filters.
问:我如何调用父视图的方法,并保持与原顶视图的方法的关系
Question: How do I call a parent view's method and keep the method's relationship with the original top view?
推荐答案
您应该引发子视图使用类似
You should trigger an event in the child view using something like
this.trigger('event', [args]);
和从父视图听吧。
this.listenTo(childView, 'event', this.paginationFilter);
(推荐给事件一个合适的名字考虑一个名称,如。页:变页是一种命名空间只是为了在大型应用程序的事件更容易的姓名和组织有没有真正的命名空间。功能。)
(Recommend giving the event a proper name. Consider a name such as 'page:change'. 'page' is a sort of namespace just to make the events in large application easier to name and organize. There is no real namespace functionality.)
这篇关于主干:如何引发父视图的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!