[英]Backbone.js view listen to jquery ui events
I'm trying to get a backbone.js view to listen for an event from jquery-ui sortable. 我正在尝试使用backbone.js视图来监听来自jquery-ui可排序的事件。 The
update
event is triggered when the user stopped sorting and the DOM position has changed. 当用户停止排序并且DOM位置已更改时,将触发
update
事件。
Here's what my view looks like: 这是我的观点:
var ProductListView = Backbone.View.extend({
el: '#products',
initialize: function(){
_.bindAll(this, 'render','addOne', 'sortChanged');
this.listenTo(app.collection.filteredProducts, 'reset', this.render);
this.listenTo(this.$el, 'update', this.sortChanged);
this.$el.sortable();
},
render: function(){
this.$el.html('');
this.collection.each(function(product){
this.addOne(product);
}, this);
return this;
},
addOne: function(product){
var view = new ProductItemView({model: product});
this.$el.append(view.render().el);
},
sortChanged: function(){
alert('changed');
}
});
I thought that this.$el
would emit an update
event since that is the same element sortable is applied to. 我认为
this.$el
会发出一个update
事件,因为它是可应用的可排序元素。 However, I'm not getting any alerts after adjusting the sort of the list, so it seems like the event is not being fired the way I would have expected. 但是,在调整列表类型之后,我没有得到任何警报,所以看起来事件没有像我预期的那样被触发。
I have read this post: 我看过这篇文章:
Saving jQuery UI Sortable's order to Backbone.js Collection 将jQuery UI Sortable的顺序保存到Backbone.js集合中
That method seems to work, but I would like to understand why this method does not. 这种方法似乎有效,但我想理解为什么这种方法没有。
Try using the views events. 尝试使用views事件。
var ProductListView = Backbone.View.extend({
el: '#products',
events: {
sortupdate: 'sortChanged'
},
...
});
According to the jQueryUI documentation , the event name is actually, "sortupdate". 根据jQueryUI文档 ,事件名称实际上是“sortupdate”。 You can add the listener one of two ways.
您可以通过两种方式添加侦听器。
Code examples: 代码示例:
Initialize the sortable with the update callback specified: 使用指定的更新回调初始化sortable:
$( ".selector" ).sortable({
update: function( event, ui ) {}
});
Bind an event listener to the sortupdate event: 将事件侦听器绑定到sortupdate事件:
$( ".selector" ).on( "sortupdate", function( event, ui ) {} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.