从BackboneJS子视图调用视图功能 [英] Call view function from subview with BackboneJS
本文介绍了从BackboneJS子视图调用视图功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能从BackboneJS子视图调用视图功能。
如果是的话,它是如何工作的?
I'd like to know if it's possible to call a view function from a subview with BackboneJS. If yes, how it's working?
我要调用属于从子视图MAINVIEW功能你好。
I want to call the function "hello" which belong to the mainView from the subview.
也许,如果事件触发...
Maybe if event triggering...
例如:
var MainView = Backbone.View.extend({
initialize: function() {
this.$template = $(template);
this.subview = new SubView();
this.render();
},
render: function() {
this.$el.html(this.$template);
var element = this.$template.attr('id');
this.subview.setElement('#'+element).render();
},
hello: function() {
alert('Hello');
}
});
var SubView = Backbone.View.extend({
initialize: function() {
this.$template = $(template);
this.render();
},
render: function() {
this.$el.html(this.$template);
//Call view function ' hello '
//parentView.hello();
}
});
谢谢!
推荐答案
您可以从父视图传递引用到你的子视图:
You can pass a reference from your parent view to your subview:
http://jsfiddle.net/puleos/hecNz/
var MainView = Backbone.View.extend({
initialize: function() {
this.$template = $("<span>foo</span>");
this.subview = new SubView({parent: this});
this.render();
},
render: function() {
this.$el.html(this.$template);
var element = this.$template.attr('id');
this.subview.setElement('#'+element).render();
},
hello: function() {
alert('Hello');
}
});
var SubView = Backbone.View.extend({
initialize: function(options) {
this.$template = $("<span>bar</span>");
this.parent = options.parent;
this.render();
},
render: function() {
this.$el.html(this.$template);
this.parent.hello();
}
});
var mainView = new MainView();
console.log(mainView);
这篇关于从BackboneJS子视图调用视图功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文