Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Supermodel does not respect {wait: true} #42

Open
johanneslumpe opened this issue Oct 27, 2013 · 0 comments
Open

Supermodel does not respect {wait: true} #42

johanneslumpe opened this issue Oct 27, 2013 · 0 comments

Comments

@johanneslumpe
Copy link

@braddunbar : When passing {wait: true} as option to the create method of a Supermodel or to the create method of an association collection, the option is not respected. The new model instance is created and will be added to the all() collections immediately.

Is this on purpose to be in line with the change of Backbone being optimistic about saves? Added success and error callbacks are called and it's easily possible to remove a model again using the following error callback:

error: function(model) {
  model.destroy();
}

But since views might be listening to events on a collection this would lead to the new model's view being rendered, just to be removed shortly afterwards in case of a failed save-attempt. (This is how it is in my case)

One way to solve this is to move the following code out of the initialize function and into it's own:

// Add the model to `all` for each constructor in its prototype chain.
var ctor = this.constructor;
do { ctor.all().add(this); } while (ctor = ctor.parent);

// Trigger 'initialize' for listening associations.
this.trigger('initialize', this);

This way we could check if the wait option is set and then just wrap the passed success callback in a function, which would first add the now saved model to the all() collections and after that trigger the initialize event.

Let me know what you think about this and I would be happy to create a pull request!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant