[英]Backbone.js change event not firing
I have a lot of select boxes each having class as browse_select
. 我有很多选择框,每个类都有
browse_select
。 I want to fire the render
function whenever any option from those select boxes is changed but it didn't fire this function. 我想在这些选择框中的任何选项被更改时触发
render
功能,但它不会激活此功能。
$(function(){
var Users = Backbone.Collection.extend({
url: "/app/phpscripts/services/browse_users/?"
});
var UserView = Backbone.View.extend({
el: '.list_ctn ul',
tagName: 'li',
events: {
"change .browse_select" : "render"
},
render: function(){
console.log("render");
var users = new Users();
var that = this;
users.fetch({
success: function(){
var template = _.template($('#myUserTemp').html(),{users: users.models});
that.$el.html(template);
}
});
}
});
var userView = new UserView();
userView.render();
});
Double check that the select boxes are in #myUserTemp. 仔细检查选择框是否在#myUserTemp中。
Also, you might want to put the user.fetch logic outside that view. 此外,您可能希望将user.fetch逻辑放在该视图之外。 Since its part of a callback I'm not sure if the event listeners can be properly set because of your flow.
由于它是回调的一部分,我不确定是否可以因为你的流程而正确设置事件监听器。
I would suggest that you call fetch from outside the view, then when the fetch is successful initiate a new UserView and pass the collection. 我建议您从视图外部调用fetch,然后在获取成功时启动新的UserView并传递集合。 That allows you to set the _.template() in the standard way as well, as a view property.
这允许您以标准方式设置_.template(),作为视图属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.