6

I am rather confused by the differences between the two. It seems like Collection.create() (fires add and sync events) can be seen as the combination of Collection.add() (fires add) and Model.save() (fires sync)?

Is the above assessment correct? What am I missing?

1 Answer 1

10

That's right. It's a shortcut method. Documentation states:

create collection.create(attributes, [options])

Convenience to create a new instance of a model within a collection. Equivalent to instantiating a model with a hash of attributes, saving the model to the server, and adding the model to the set after being successfully created.

And annotated source code:

Create a new instance of a model in this collection. Add the model to the collection immediately, unless wait: true is passed, in which case we wait for the server to agree.

This second description is a little bit more accurate as only passing {wait:true} has the effect of not adding a model to the collection in case of errors upon saving the model.

1
  • Is the model instance created added to the client side collection if and only if the server side addition successful?
    – sçuçu
    Dec 17, 2014 at 20:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.