modify pager widget to handle boundary conditions; now takes a Dataset instead of a Query so that it has access to the record count; add tests

This commit is contained in:
kielni
2014-01-17 13:07:41 -08:00
parent 45fa438803
commit 8c1a1359db
5 changed files with 120 additions and 11 deletions

View File

@@ -260,7 +260,7 @@ my.MultiView = Backbone.View.extend({
}, this);
this.pager = new recline.View.Pager({
model: this.model.queryState
model: this.model
});
this.$el.find('.recline-results-info').after(this.pager.el);

View File

@@ -25,34 +25,43 @@ my.Pager = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model.queryState, 'change', this.render);
this.render();
},
onFormSubmit: function(e) {
e.preventDefault();
var newFrom = parseInt(this.$el.find('input[name="from"]').val());
newFrom = Math.min(this.model.recordCount, Math.max(newFrom, 1))-1;
var newSize = parseInt(this.$el.find('input[name="to"]').val()) - newFrom;
newFrom = Math.max(newFrom, 0);
newSize = Math.max(newSize, 1);
this.model.set({size: newSize, from: newFrom});
newSize = Math.min(Math.max(newSize, 1), this.model.recordCount);
this.model.queryState.set({size: newSize, from: newFrom});
},
onPaginationUpdate: function(e) {
e.preventDefault();
var $el = $(e.target);
var newFrom = 0;
var currFrom = this.model.queryState.get('from');
var size = this.model.queryState.get('size');
var updateQuery = false;
if ($el.parent().hasClass('prev')) {
newFrom = this.model.get('from') - Math.max(0, this.model.get('size'));
newFrom = Math.max(currFrom - Math.max(0, size), 1)-1;
updateQuery = newFrom != currFrom;
} else {
newFrom = this.model.get('from') + this.model.get('size');
newFrom = Math.max(currFrom + size, 1);
updateQuery = (newFrom < this.model.recordCount);
}
if (updateQuery) {
this.model.queryState.set({from: newFrom});
}
newFrom = Math.max(newFrom, 0);
this.model.set({from: newFrom});
},
render: function() {
var tmplData = this.model.toJSON();
tmplData.to = this.model.get('from') + this.model.get('size');
var from = parseInt(this.model.queryState.get('from'));
tmplData.from = from+1;
tmplData.to = Math.min(from+this.model.queryState.get('size'), this.model.recordCount);
var templated = Mustache.render(this.template, tmplData);
this.$el.html(templated);
return this;
}
});