AmpersandJS / ampersand-model

Observable objects, for managing state in applications.
MIT License
84 stars 31 forks source link

Out of order responses for `save` lose changes? #33

Closed remko closed 9 years ago

remko commented 9 years ago

Suppose that I do the following:

model.save({foo: 1});
model.save({foo: 2});

Now suppose that the first save takes the server longer to complete than the second one. In that case, the callback to update the model with the server state will first update the model with foo: 2, and then with foo: 1, discarding the second save. Is this known? This seems to be an actual issue for me.

What's the way to handle parallel PUT operations with Ampersand? Is it at all possible?

remko commented 9 years ago

Thinking about it a bit longer, I guess one solution would be to make sure that save doesn't do simultaneous requests, and queue the last one up until the current one is finished.

pgilad commented 9 years ago

You need to decide what your request strategy is:

What you are doing is a race, and it has it's benefits, if that's what you want to do.

sequential saves are easy to do, just chain the promises (or success callbacks).

parallel saves are impossible to do on PUT requests, only on PATCH, assuming you are modifying the entire model.

Other than that, I don't see a solution to this ending up in this repository, perhaps in a different one, as to keep this about core functions. Most users don't deal with these edge cases..