updated backgrid, pageable

This commit is contained in:
kay.one 2013-05-12 08:22:54 -07:00
parent 30a55c465a
commit 9515064f14
4 changed files with 616 additions and 282 deletions

View File

@ -50,6 +50,15 @@
Backbone.View.prototype.initialize.apply(this, arguments);
this.name = options.name || this.name;
this.placeholder = options.placeholder || this.placeholder;
var collection = this.collection, self = this;
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection &&
collection.mode == "server") {
collection.queryParams[this.name] = function () {
return self.$el.find("input[type=text]").val();
};
}
},
/**
@ -59,9 +68,8 @@
*/
search: function (e) {
if (e) e.preventDefault();
var $text = $(e.target).find("input[type=text]");
var data = {};
data[$text.attr("name")] = $text.val();
data[this.name] = this.$el.find("input[type=text]").val();
this.collection.fetch({data: data});
},

File diff suppressed because it is too large Load Diff

View File

@ -52,23 +52,12 @@
Initializer.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
Column metadata.
@param {Backbone.Collection} options.collection
@param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons.
*/
initialize: function (options) {
//Backgrid.requireOptions(options, ["columns", "collection"]);
Backgrid.requireOptions(options, ["collection"]);
this.columns = options.columns;
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Backgrid.Columns(this.columns);
}
var columns = this.columns;
this.listenTo(columns, "add", this.render);
this.listenTo(columns, "remove", this.render);
this.listenTo(columns, "change:renderable", this.render);
var collection = this.collection;
var fullCollection = collection.fullCollection;
if (fullCollection) {
@ -92,31 +81,35 @@
changePage: function (e) {
e.preventDefault();
var label = $(e.target).text();
var ffLabels = this.fastForwardHandleLabels;
var $li = $(e.target).parent();
if (!$li.hasClass("active") && !$li.hasClass("disabled")) {
var collection = this.collection;
var label = $(e.target).text();
var ffLabels = this.fastForwardHandleLabels;
if (ffLabels) {
switch (label) {
case ffLabels.first:
collection.getFirstPage();
return;
case ffLabels.prev:
if (collection.hasPrevious()) collection.getPreviousPage();
return;
case ffLabels.next:
if (collection.hasNext()) collection.getNextPage();
return;
case ffLabels.last:
collection.getLastPage();
return;
var collection = this.collection;
if (ffLabels) {
switch (label) {
case ffLabels.first:
collection.getFirstPage();
return;
case ffLabels.prev:
collection.getPreviousPage();
return;
case ffLabels.next:
collection.getNextPage();
return;
case ffLabels.last:
collection.getLastPage();
return;
}
}
}
var state = collection.state;
var pageIndex = $(e.target).text() * 1;
collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex);
var state = collection.state;
var pageIndex = +label;
collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex);
}
},
/**
@ -132,12 +125,13 @@
var state = collection.state;
// convert all indices to 0-based here
var lastPage = state.lastPage ? state.lastPage : state.firstPage;
lastPage = state.firstPage === 0 ? lastPage : lastPage - 1;
var currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
var firstPage = state.firstPage;
var lastPage = +state.lastPage;
lastPage = Math.max(0, firstPage ? lastPage - 1 : lastPage);
var currentPage = Math.max(state.currentPage, state.firstPage);
currentPage = firstPage ? currentPage - 1 : currentPage;
var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
var windowEnd = windowStart + this.windowSize;
windowEnd = windowEnd <= lastPage ? windowEnd : lastPage + 1;
var windowEnd = Math.min(lastPage + 1, windowStart + this.windowSize);
if (collection.mode !== "infinite") {
for (var i = windowStart; i < windowEnd; i++) {
@ -185,8 +179,7 @@
},
/**
Render the paginator handles inside an unordered list placed inside a
cell that spans all the columns.
Render the paginator handles inside an unordered list.
*/
render: function () {
this.$el.empty();

View File

@ -1,5 +1,5 @@
/*
backbone-pageable 1.2.2
backbone-pageable 1.2.3
http://github.com/wyuenho/backbone-pageable
Copyright (c) 2013 Jimmy Yuen Ho Wong
@ -65,6 +65,7 @@
var _isUndefined = _.isUndefined;
var _result = _.result;
var ceil = Math.ceil;
var floor = Math.floor;
var max = Math.max;
var BBColProto = Backbone.Collection.prototype;
@ -303,6 +304,8 @@
state.firstPage :
state.currentPage;
if (!_isArray(models)) models = models ? [models] : [];
if (mode != "server" && state.totalRecords == null && !_isEmpty(models)) {
state.totalRecords = models.length;
}
@ -848,6 +851,28 @@
return this.fetch(_omit(options, "fetch"));
},
/**
Fetch the page for the provided item offset in server mode, or reset the current page of this
collection to the page for the provided item offset in client mode.
@param {Object} options {@link #getPage} options.
@chainable
@return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest
from fetch or this.
*/
getPageByOffset: function (offset, options) {
if (offset < 0) {
throw new RangeError("`offset must be > 0`");
}
offset = finiteInt(offset);
var page = floor(offset / this.state.pageSize);
if (this.state.firstPage !== 0) page++;
if (page > this.state.lastPage) page = this.state.lastPage;
return this.getPage(page, options);
},
/**
Overidden to make `getPage` compatible with Zepto.