Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js model.save() sending multiple PUT requests

I have a backbone view that loads a model and some templates. When I submit the form in the edit template, backbone successfully sends a PUT request, just as it’s supposed to. On success, I navigate the user back to the view template.

However, if I navigate to the edit route again and submit the form, backbone sends two PUT requests. It then GETs the view template. If I navigate to the edit route a third time, backbone sends three PUT requests. The number of PUT requests keep incrementing every time I submit the form. Why might that be?

Here is my view:

// Filename views/users/edit.js
/*global define:false */

define([
    'jquery',
    'underscore',
    'backbone',
    'models/user/UserModel',
    'text!templates/users/edit.html',
], function($, _, Backbone, UserModel, UserTemplate) {

    var UserEdit = Backbone.View.extend({
        el: '#page',
        render: function (options) {

            var that = this;

            if (options.id) {
                // modify existing user
                var user = new UserModel({id: options.id});
                user.fetch({
                    success: function (user) {
                        var template = _.template(UserTemplate, {user: user});
                        that.$el.animate({opacity: 0}, 180, function() {
                            that.$el.html(template).animate({opacity: 1}, 180);
                        });
                    }
                });
            } else {
                // create new user
                var template = _.template(UserTemplate, {user: null});
                that.$el.animate({opacity: 0}, 180, function() {
                    that.$el.html(template).animate({opacity: 1}, 180);
                });
            }

        },
        events: {
            'submit #create-user-form': 'createUser'
        },
        createUser: function (e) {
            var postData = $(e.currentTarget).serializeObject();
            var user = new UserModel();
            user.save(postData, {
            success: function (user, response) {
                Backbone.history.navigate('#/users/view/' + response, {trigger: true, replace: true});
                }
            });
            return false;
        }
    });

    return UserEdit;

});
like image 496
Jezen Thomas Avatar asked Feb 16 '23 00:02

Jezen Thomas


1 Answers

In my case, I could fix it by calling undelegateEvents() on the view in the success callback.

createUser: function (e) {
    var postData = $(e.currentTarget).serializeObject(),
            user = new UserModel(),
            that = this;

    user.save(postData, {
        success: function (user, response) {
            that.undelegateEvents();
            Backbone.history.navigate('#/users/view/' + response, {trigger: true});
        }
    });

    return false;
}

Thanks, @dbf.

like image 71
Jezen Thomas Avatar answered Feb 22 '23 21:02

Jezen Thomas