We created a Collection of models from an array as given below. Our question is: will the Collection preserve the same order of elements/models as it was present in the original array? In other words, is the same order (of source array) guaranteed in Collection?
var colorsData = [
{
name: 'red'
},
{
name: 'blue'
},
{
name: 'green'
},
.....
.....
.....
//other colors
];
var Color = Backbone.Model.extend({
defaults: {
name: 'white'
}
});
var ColorCollection = Backbone.Collection.extend({
model: Color
});
var colorCollection1 = new ColorCollection(colorsData); //creating a collection from the source array colorsData.
From all that I can gather, yes a
Collectionpreserves the original order of elements.You can read the annotated source for collection to see for yourself that it isn’t magically shuffled time to time. Such functions as
at,unshift,pop,push, etc. are quite clear indications of this.You can read the annotated source for
parse, which is used to parse the response from server after for examplefetch-oprations:To ascertain that whatever is passed on to your collection won’t be shuffled anywhere. The resp will be passed on to the
add-function that will process the response in natural order.