Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js `model.destroy()` custom transitions?

When I use Backbone's model.destroy(), it seems to automatically remove that view from the DOM.

Is there a way for me to use destroy() to send the DELETE request, but remove the view from the DOM myself?

Something like:

this.model.destroy({
    wait: true,
    success: function(){
        $('#myElement').animate({
            "height" : "0",
            1000,
            function(){$('#myElement').remove()}
        });
    }
});
like image 632
Garrett Boatman Avatar asked Apr 24 '15 23:04

Garrett Boatman


People also ask

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is the difference between angular and backbone JS?

AngularJS is a powerful Javascript based standalone framework, whereas Backbone. js is a lightweight javascript framework. AngularJS uses a two-way data binding process, whereas Backbone. js doesn't provide any data binding process and so it is not suitable for large web page development.

Is Backbone JS frontend or backend?

Backend Synchronization BackboneJS is use with the front-end and back-end systems, allows the synchronization with the backend to provide support to RESTful APIs.


2 Answers

You need to override _onCollectionRemove() in whichever Collection view contains the item views (documentation). This is the function which is called when your model is removed from the collection, and it's also what's destroying your view. Specifically how you choose to override it is up to you, but it might be easiest to override it with your animation function, maybe along the following lines...

_onCollectionRemove: function(model) {
  var view = this.children.findByModel(model);
  var that = this;
  view.$('#myElement').animate({
        "height" : "0",
        1000,
        function(){
            that.removeChildView(view);
            that.checkEmpty();
        }
    });
}

If you prefer to handle the removal of the view manually in your destroy callback, just override _onCollectionRemove() to contain an empty function and do whatever you'd like in the callback of your delete request. I'd recommend the approach I describe above rather than doing it in your destroy callback, though. Completely eliminating the function and then handling it's responsibilities elsewhere in your code interferes with Marionette's intended event flow. Simply overriding the function with a different UI effect preserves the natural flow.

EDIT: Another user's previous answer (now deleted due to downvoting) suggested that it might be wise to call destroy after the UI effect was completed. This is not a good approach for the reason OP pointed out - if something goes wrong with the destroy method, (for example, if the remote server goes down) it appears to the user as if the model was deleted (the UI effect had already completed) even though the server was unreachable and the model remains.

like image 90
Michael.Lumley Avatar answered Oct 02 '22 14:10

Michael.Lumley


the mentioned onBeforeDestroy method does not work for me. It throws an error in backbone (remove method missing) My solution has the same aproach and is working very well in itemView

remove: function(){

    this.$el.animate({"height" : "0"},500, function(){
        $(this).remove();
    });

},
like image 31
Volker Schmitt Avatar answered Oct 02 '22 15:10

Volker Schmitt