Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collection.create should return the request object #2961

Closed
iros opened this issue Jan 16, 2014 · 5 comments
Closed

collection.create should return the request object #2961

iros opened this issue Jan 16, 2014 · 5 comments

Comments

@iros
Copy link
Contributor

iros commented Jan 16, 2014

Right now the current create call returns the model that was created, but that is inconsistent with the way save, destroy and fetch work. Those all return the xhr object, allowing the use of deferreds in a consistent fashion. Since create is also a networked operation, it should return the xhr object as well.

Is there a reason why create returns the model itself? I realize that you can provide a json object at which point you'd need to capture the model instance in the callback, but you could also pass a model directly to create (thus already having the model). At present, I believe create is the only api call that requires use of the success callback if you want to capture the result of the operation.

Happy to make a pull req if this is a desired change.

@tgriesser
Copy link
Collaborator

I agree, the model returned from create isn't too useful since you don't really know what the deal is with it until you hear from the server, and it'd be more consistent to get the xhr object for the promise chaining, etc... but #1155 and #2220 have some info about the reasoning for it.

@iros
Copy link
Contributor Author

iros commented Jan 16, 2014

So far the reasoning I see is that "it's intended to return a model" which isn't really a reason in it of itself. Is there a specific use case that necessitates it behaving differently from the others?

@jashkenas
Copy link
Owner

create is a shortcut for "new model", plus "add to collection and save". If create returned the XHR, you'd have no reference to the model you just made. If you want the XHR, use new, and then save it later.

@iros
Copy link
Contributor Author

iros commented Jan 16, 2014

but @jashkenas, you could pass a model to create that has been created elsewhere. Additionally, you could:

var myModel = null;
collection.create({ bla: 12 }).then(function(model) {
  myModel = model;
});

An empty non-initialized model is worse than no model, because someone not realizing there is a network operation happening could start using the model before it's actually saved, which may not be the desired behavior.

@jashkenas
Copy link
Owner

you could pass a model to create that has been created elsewhere

You could, but that's not the intended use case.

Your code example makes sense, but it's noticeably awkward compared to:

var model = collection.create({bla: 12})

someone not realizing there is a network operation happening could start using the model before it's actually saved

... that's exactly how optimistic models are supposed to be used. If you're worried about the save failing, or the model being invalidated by the server, and so on, then you'll want to wait for the save to complete.

create is just a shortcut — think create in Rails.

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

No branches or pull requests

3 participants