[英]How can I Pass a Custom function from Router, to View, to Template in Backbone.js?
I have a Backbone Router and six Backbone Views which load (depending on User's settings) one of two templates. 我有一个Backbone路由器和六个Backbone视图加载(取决于用户的设置)两个模板之一。 Yet, both templates use the same function, and I don't want to repeat myself defining the function in both templates, or in the six Views.
然而,两个模板都使用相同的功能,我不想重复自己在两个模板或六个视图中定义功能。 Instead, I would like to define it in the Router.
相反,我想在路由器中定义它。 I know how to pass a model in to a view and template.
我知道如何将模型传递给视图和模板。 But, how can I pass my custom function through to the View and then to the templates so that it can be called in either of them?
但是,我如何将自定义函数传递给View,然后传递给模板,以便可以在其中任何一个中调用它?
Thanks 谢谢
You could create a "super" view that all your other views extend. 您可以创建一个“超级”视图,其他所有视图都可以扩展。
Something like: 就像是:
var MySuperView = Backbone.View.extend({
myReusableFunction: function() {
// do stuff
}
});
var View1 = MySuperView.extend({});
var View2 = MySuperView.extend({});
Both View1 and View2 will have myReusableFunction
available. View1和View2都有
myReusableFunction
可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.