A collection in Backbone can consist of various models that are defined within it.
When setting up your collection, you would typically do so as follows:
YourCollection = Backbone.Collection.extend({
model: YourModel,
url: '/url/to/json/collection';
}
});
And for the model:
YourModel = Backbone.Model.extend({
url: '/url/to/json/model';
}
});
With this setup, you can then perform actions like:
var collection = new YourCollection();
collection.fetch(); //GETs /url/to/json/collection
The path /url/to/json/collection
is expected to return a JSON array where each element represents a model in your collection.
Likewise, the path /url/to/json/model
should return JSON data representing a single model.
If, for example, your JSON response includes a property called "name"
, you could utilize functions like
collection.where({name: 'some name'})
to filter and retrieve specific models with matching names.
In conclusion, loading your data using Backbone's collection is highly recommended.