Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js - problem when saving a model before previous save issues POST(create) instead of PUT(update) request
    text
    copied!<p>I've developed a nice rich application interface using <a href="http://documentcloud.github.com/backbone/" rel="noreferrer">Backbone.js</a> 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.</p> <p>An example of how to recreate this problem is as follows:</p> <ol> <li><p>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.</p> <p><code>person = new Person();</code></p></li> <li><p>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.</p> <p><code>person.set ({ name: 'John' });</code></p> <p><code>person.save(); // create new model</code></p></li> <li><p>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.</p> <p><code>person.set ({ age: 20 });</code></p> <p><code>person.save(); // update the model</code></p></li> </ol> <p>So we would expect in this scenario one POST request to create the model, and one PUT requests to update the model.</p> <p>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.</p> <p>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.</p> <p>Your advice on how to deal with this issue would be appreciated.</p> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload