绑按钮onclick事件骨干查看 [英] tying button onclick event to Backbone View
问题描述
我有一个样品中骨干单页的应用程序,我与瞎搞。我们的想法是,我有一个按钮,将触发一个视图刷新。我试图让事件处理程序记住'这'为骨干视图,而不是它被称为元素上。不管我有多文档阅读,我似乎无法让过去的这个心理驼背。
I have a sample single-page-application in Backbone that I am messing around with. The idea is that I have a button that will trigger a refresh on a view. I am trying to get the event handler to remember 'this' as the backbone view, not the element that it was called on. No matter how much docs I read, I cant seem to make it past this mental hump.
在我看来,我有
initialize: function() {'
//i am trying to say, call render on this view when the button is clicked. I have tried all of these calls below.
//this.$("#add-tweet-button").click(this.render);
//this.$("#add-button").bind("click", this.render);
}
当呈现功能被调用时,这个元素是按钮。我知道我在想念是pretty容易,有人可以帮助我吗?此外,这是声音编码约定去了?
When the render function is called, the 'this' element is the button. I know what im missing is pretty easy, can someone help me out with it? Also, is this sound as coding conventions go?
推荐答案
如果您使用视图的的 delegateEvents 功能,该作用域是照顾你:
If you use the View's 'delegateEvents' functionality, the scoping is taken care of for you:
var yourView = Backbone.View.extend({
events: {
"click #add-tweet-button" : "render"
},
render: function() {
// your render function
return this;
}
});
这仅适用于那些'下'视图的EL元件。但是,你的例子说明这一点。$(...),所以我假设是这种情况。
This only works with elements that are 'under' the View's El. But, your example shows this.$(...), so I'm assuming this is the case.
这篇关于绑按钮onclick事件骨干查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!