I have a tabular data view with ability to sort on each individual column. As data being huge, I prefer to show few rows in the page and load rows on demand. So wheneever user sorts a column in page, a new request is made to server and new data rows are fetched to display. But User can modify sort type(asc or desc) of two or more columns at a time with pop up page allowing it to modify sort type for more columns. I tried to solve this use case with a collection of metadata (asc/desc and other information) for columns.
var ColumnsMetadata = Backbone.Collection.extend({
model: ColMetadata
});
var ColMetadata = Backbone.Model.extend({
defaults: {
sort: 'none'
}
});
A EditSortView
page to present and edit the column sort info. And a TableView
to display tablular data. The TableView
and EditSortview
both share ColumnsMetadata
.
var EditSortView = Backbone.View.extend({
initialize: function() {
//this.colleciton = columnMetadataCollection
},
events: function() {
"change input.sortVal" : "_updateSortMetdata",
"click button#sort": '_sort'
},
_updateSortMetdata: function() {
// updates right ColMetadata instance' sort property
},
_sort: funciton() {
// should make fetch request only when user finally
// satisfy with sort order changes
}
});
var TableView = Backbone.View.extend({
initialize: function() {
// other set up code
// i could add listener on collection
this.listenTo(this.columnMetadataCollection, 'change', this._fetchAndUpdate);
},
_fetchAndUpdate: function() {
// uses columnMetadataCollection to fetch data
}
});
As I added change
listener on columnMetadataCollection
in TableView, for each colMetadata
change (from EditSortView popup) would trigger a new request to server. But it is optimal to make only one request to server when user satisfies with changes made.
How can I listen to columnMetadataCollection
in TableView so that only a request is made to sever? Or how can I structure my code with Backbone to make it right?