1

The below code is in a Backbone.View within the render function. I am using Backbone.Relational and 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);

}
});

The problem is happening in the sortable remove function. The element that has left the sortable contains its corresponding model in its .data(). So we retrieve it from there and try to remove it from this collection but when logging the collection afterwards the model never gets removed. What makes this especially strange is that the receive function above it which uses the same exact model to add works perfectly. Any ideas? I appreciate the help!

Rick
  • 61
  • 1
  • 4
  • 1
    Not sure if relevant or just a mistake copying it here, but in `receive` you appear to be adding the `productView` to the collection, and in `remove` removing `productView.model`. – jevakallio Jan 12 '13 at 01:37
  • Good spot fencliff. Sadly that was just a copying error. I have edited the post. The problem still occurs... – Rick Jan 14 '13 at 23:59
  • I dove into the backbone relational code and by removing a line there I fixed the problem I was having. I have submitted a bug on their github: https://github.com/PaulUithol/Backbone-relational/issues/243 More than likely it's something I'm doing wrong but hopefully submitting a bug will get some attention to the issue :) – Rick Jan 15 '13 at 01:09

0 Answers0