1

I am showing rows from my collection in a datatable. I have a view in which I destroy a model and once that's done I need to remove the corresponding row in datatable. I am able to destroy the model but don't know how to remove that specific row. The view that handles the rows is like this:

var rowRow = Backbone.View.extend({
  tagName: "tr",
  events: {
    'click .edit':'editrow',
    'click .delete':'deleterow'
  },

  render: function() {
    data = this.model.toJSON();

    this.$el.html('<td>'+data.name+'</td><td>'+data.email+'</td><td>'+data.contact_number+'</td><td><span id="delete'+data.id+'" class="delete">Delete</span> | <span class="edit">Edit</span></td>');
    return this;
  },

  editrow: function(){
    alert("edit");
  },

  deleterow: function() {
    var data = this.model.toJSON();

    this.model.destroy({ wait: true, success: function(){ }});
  }
})

Just after destruction of model, how can I remove that specific row in datatable?

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
beNerd
  • 3,314
  • 6
  • 54
  • 92

1 Answers1

1

I suppose this.$el.remove() should remove the whole row from the DOM.

A post on Stack put this code also : (https://stackoverflow.com/a/11534056/968988)

destroy_view: function() {

    //COMPLETELY UNBIND THE VIEW
    this.undelegateEvents();

    this.$el.removeData().unbind(); 

    //Remove view from DOM
    this.remove();  
    Backbone.View.prototype.remove.call(this);
}

I think the next version of Backbone will have an helper for this.

Community
  • 1
  • 1
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
  • i am actually using datatables. so there is fnDeleteRow like function but dont know how to call it here as it will be binded in table itself and how will i be able to refer it. – beNerd Feb 03 '13 at 04:26
  • datatables is a (great) DOM oriented library where Backbone is a "memory" library. You should first use datatable.fnDeleteRow() then delete the backbone objects (this.model.destroy(), this.view.destroy_view) – Nicolas Zozol Feb 04 '13 at 06:28