Answer the question
In order to leave comments, you need to log in
How to properly use collections in Backbone?
Help me understand Backbone.
In which cases should you use models, and in which cases is it better to use collections?
If, for example, you need to take an array of data from the server and you just need to display it in the form, is it better to use a collection or a model for this?
You can also give examples of when it is correct to use the model, and when the collection.
Thank you all in advance for your replies.
Answer the question
In order to leave comments, you need to log in
Model and collection are primarily abstractions over data. The meaning of these abstractions is the convenience of data manipulation. Are you manipulating data? No. Just render the array. Therefore, in this case, you can get by with the model. But if you need to work with an array not as a single entity, but as a set of entities, then you need to take a collection.
If I remember correctly, the logic is something like this:
Let's say we have a basket of which there are orders, each order has its own parameters (name, price, quantity) this is a model, and the basket is a storage for several models, i.e. . a collection(of models) that performs actions on models. Then we can, for example, tell the collection to do some kind of action with the models
. In your case, you most likely just need a model.
A collection is a table in a database, a model is a row in that table.
Unfortunately, the real world is sometimes more complex, and there are relationships like one-to-many, many-to-many, and so on. In relation to Backbone.js, this means that you have a collection (or another model) nested in your model. There are several plugins for dealing with such situations, but perhaps the most powerful is Backbone-relational.js
as soon as you meet the word array, we immediately use the collection
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question