Create a model that contains ordering field
var Song = Backbone.Model.extend({
defaults: {
trackNumber: 1
}
});
Define the comparator method on your collection. You can look to the official backbone documentation for detailed information about comparator method.
var Album = Backbone.Collection.extend({
model: Song,
comparator: function (model) {
return model.get("trackNumber")
}
});
Here is the example collection:
var album = new Album([
{"trackNumber": 1, "name": "Freefall"},
{"trackNumber": 2, "name": "Supertwister"},
{"trackNumber": 3, "name": "Nimrodel/The process..."},
{"trackNumber": 4, "name": "Earthrise"},
{"trackNumber": 5, "name": "Lady Fantasy"}
]);
And initialize the SortableView in your view.
new SortableView({
model: album,
orderField: "trackNumber",
itemTemplate: "<%= name %>"
}).render()
The `render` method will be returned unattached DOM element.
| Parameter | Type | Default | Description |
|---|---|---|---|
| orderField | string | order | The ordering field of your model. |
| itemTemplate | string | Empty string | Sortable item template. |
| overClass | string | over | The class name of the of over state of sortable items |