Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6041843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:43:12+00:00 2026-05-23T06:43:12+00:00

I’ve developed a nice rich application interface using Backbone.js where users can add objects

  • 0

I’ve developed a nice rich application interface using Backbone.js where users can add objects very quickly, and then start updating properties of those objects by simply tabbing to the relevant fields. The problem I am having is that sometimes the user beats the server to its initial save and we end up saving two objects.

An example of how to recreate this problem is as follows:

  1. User clicks the Add person button, we add this to the DOM but don’t save anything yet as we don’t have any data yet.

    person = new Person();

  2. User enters a value into the Name field, and tabs out of it (name field loses focus). This triggers a save so that we update the model on the server. As the model is new, Backbone.js will automatically issue a POST (create) request to the server.

    person.set ({ name: 'John' });

    person.save(); // create new model

  3. User then very quickly types into the age field they have tabbed into, enters 20 and tabs to the next field (age therefore loses focus). This again triggers a save so that we update the model on the server.

    person.set ({ age: 20 });

    person.save(); // update the model

So we would expect in this scenario one POST request to create the model, and one PUT requests to update the model.

However, if the first request is still being processed and we have not had a response before the code in point 3 above has run, then what we actually get is two POST requests and thus two objects created instead of one.

So my question is whether there is some best practice way of dealing with this problem and Backbone.js? Or, should Backbone.js have a queuing system for save actions so that one request is not sent until the previous request on that object has succeeded/failed? Or, alternatively should I build something to handle this gracefully by either sending only one create request instead of multiple update requests, perhaps use throttling of some sort, or check if the Backbone model is in the middle of a request and wait until that request is completed.

Your advice on how to deal with this issue would be appreciated.

And I’m happy to take a stab at implementing some sort of queuing system, although you may need to put up with my code which just won’t be as well formed as the existing code base!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:43:13+00:00Added an answer on May 23, 2026 at 6:43 am

    I have tested and devised a patch solution, inspired by both @Paul and @Julien who posted in this thread. Here is the code:

    (function() {
      function proxyAjaxEvent(event, options, dit) {
        var eventCallback = options[event];
        options[event] = function() {
          // check if callback for event exists and if so pass on request
          if (eventCallback) { eventCallback(arguments) }
          dit.processQueue(); // move onto next save request in the queue
        }
      }
      Backbone.Model.prototype._save = Backbone.Model.prototype.save;
      Backbone.Model.prototype.save = function( attrs, options ) {
        if (!options) { options = {}; }
        if (this.saving) {
          this.saveQueue = this.saveQueue || new Array();
          this.saveQueue.push({ attrs: _.extend({}, this.attributes, attrs), options: options });
        } else {
          this.saving = true;
          proxyAjaxEvent('success', options, this);
          proxyAjaxEvent('error', options, this);
          Backbone.Model.prototype._save.call( this, attrs, options );
        }
      }
      Backbone.Model.prototype.processQueue = function() {
        if (this.saveQueue && this.saveQueue.length) {
          var saveArgs = this.saveQueue.shift();
          proxyAjaxEvent('success', saveArgs.options, this);
          proxyAjaxEvent('error', saveArgs.options, this);
          Backbone.Model.prototype._save.call( this, saveArgs.attrs, saveArgs.options );
        } else {
          this.saving = false;
        }
      }
    })();
    

    The reason this works is as follows:

    1. When an update or create request method on a model is still being executed, the next request is simply put in a queue to be processed when one of the callbacks for error or success are called.

    2. The attributes at the time of the request are stored in an attribute array and passed to the next save request. This therefore means that when the server responds with an updated model for the first request, the updated attributes from the queued request are not lost.

    I have uploaded a Gist which can be forked here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.