以下代码位于render函数中的Backbone.View中。我正在使用Backbone.Relational和jQuery UI Sortable。
var scopedThis = this;
// make this container a sortable container
this.$el.sortable({
connectWith: '.wlProductsInRoomContainer',
revert: true,
stop: function(){
scopedThis.getNewSortOrders();
},
receive: function(e, r){
// get model from newly passed in product
var prodModel = r.item.data().productView.model;
// add model to this collection but pass silent so collection add event doesn't get ran
scopedThis.collection.add(prodModel, { silent: true });
},
remove: function (e, r){
// get model from product that has left this sortable
var prodModel = r.item.data().productView.model;
// remove this model from this collection
scopedThis.collection.remove(prodModel);
console.log(scopedThis.collection);
}
});
问题出现在可排序的删除功能中。已离开sortable的元素在其.data()中包含其对应的模型。所以我们从那里检索它并尝试从这个集合中删除它,但是在之后记录集合时,模型永远不会被删除。是什么让这个特别奇怪的是它上面的接收函数使用相同的精确模型来完美地工作。有任何想法吗?我很感激帮助!