简体   繁体   English

如何在Backbone.js中将自定义功能从路由器,视图传递到模板?

[英]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.

相关问题 如何将参数传递给Backbone.js视图? - How can I pass a parameter into a Backbone.js view? 将变量从Backbone.js路由器初始化函数传递到另一个路由器函数的forEach语句 - pass variable from Backbone.js router initialize function to the forEach statement of another router function 如何在 Backbone.js 中刷新视图/模板 - How to refresh a view/template in Backbone.js Requirejs + Backbone.js:TypeError:无法通过路由器 - 路由器不是一个功能? - Requirejs + Backbone.js: TypeError: Can't pass in the router - Router is not a function? 我如何在ID的click事件上在视图上调用函数,函数是写在骨架中的controller.js中的 - How can i call function on view on the click event of Id, function is written on controller in backbone.js 如何在同一视图中的Openlayers事件中在bone.js视图中调用函数? - How do I call a function in my backbone.js view from my Openlayers event in that same view? 在Backbone.js中如何从另一个视图调用一个视图中的函数? - In Backbone.js how do i call a function in one view from another view? 如何使用骨干.js中的路由器切换视图 - How to switch view using router in backbone.js 在骨干视图Backbone.js中调用另一个函数 - calling one function from another in backbone view Backbone.js 在Backbone.js中,如何从URL提取数据? - In Backbone.js, how can I fetch data from a URL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM