2

I'm trying to destroy a backbone model and it gets destroyed even on a service error.

View : 
this.listenTo(this.collection, 'remove', function() {});

this.model.destroy({
   success : function(model) { 
       /* remove the li view */
   },
   error : function() {}

});

In above case model is removed from collection whether it is a success/error.

Suppose if this destroy callback fails the model should not be removed from the collection. How can this be achieved?

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
user1184100
  • 6,742
  • 29
  • 80
  • 121

1 Answers1

3

Pass {wait: true} if you'd like to wait for the server to respond before removing the model from the collection:

View : 
this.listenTo(this.collection, 'remove', function() {});

this.model.destroy({
   success : function(model) { 
       /* remove the li view */
   },
   error : function() {},
   wait:true

});
Protostome
  • 5,569
  • 5
  • 30
  • 45
  • Do you want to remove a model from the collection after a certain timeout? or deal with request time out? – Protostome May 10 '13 at 07:24
  • Try passing `timeout:` at the options hash. I haven't tried it myself, but I think these properties are fed to jquery's ajax function (that accepts timeout:...) – Protostome May 10 '13 at 10:23
  • I tried adding timeout it didn't work. I also overrode the sync within the model .. App.Models.MyModel = Backbone.Model.extend({ urlRoot : '../SampleService/api/ sync: function(method, model, options) { options.timeout = 10000; }); even this didn't work. – user1184100 May 10 '13 at 10:41
  • mm... that's strange. You could also set a global timeout for your entire application, could you see if that thing works? (At least for the moment so we can figure out where the problem is exactly) `$.ajaxSetup({timeout:50000});` – Protostome May 10 '13 at 12:26
  • @Protostome Thank you for your wonderful answer.. 1 for you. but I want to romve the model only if response is positive means if it goes to success callback. – Nikhil Agrawal Oct 01 '13 at 10:06
  • @NikhilAgrawal - Isn't it the default behavior? – Protostome Oct 01 '13 at 10:22
  • @Protostome No weather My web service is working or not the model gets removed and removed from view. Please help. Can you for chat for same time. – Nikhil Agrawal Oct 01 '13 at 10:24