Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone - Difference between Collection.add() / Collection.create()?

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?

like image 264
gsklee Avatar asked Sep 02 '12 09:09

gsklee


People also ask

What is backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

Is backbone a MVC?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


1 Answers

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.

like image 80
zifot Avatar answered Sep 29 '22 10:09

zifot