From 8579ce7dce378f49851d49191fafa5b5ca843a8b Mon Sep 17 00:00:00 2001
From: Gerasimos Tzoganis
Date: Sat, 10 Aug 2013 22:10:00 +0300
Subject: [PATCH 1/4] A fix for Issue #10: actionClassName callback is not
passed the corresponding model.
---
dist/backbone.datagrid.js | 8 ++++++--
dist/backbone.datagrid.min.js | 2 +-
src/views/cells/action-cell.js | 6 +++++-
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/dist/backbone.datagrid.js b/dist/backbone.datagrid.js
index 59ad8a2..547688c 100644
--- a/dist/backbone.datagrid.js
+++ b/dist/backbone.datagrid.js
@@ -344,7 +344,7 @@ var Table = Datagrid.Table = ComposedView.extend({
this.pager = this.options.pager;
this.sorter = this.options.sorter;
- this.listenTo(this.collection, 'reset', this.render);
+ this.listenTo(this.collection, 'reset sort', this.render);
},
render: function() {
@@ -687,7 +687,11 @@ var ActionCell = Datagrid.ActionCell = Cell.extend({
a.html(this.options.label);
a.attr('href', this.options.href || '#');
if (this.options.actionClassName) {
- a.addClass(this.options.actionClassName);
+ var actionClassName = this.options.actionClassName;
+ if (_.isFunction(actionClassName)) {
+ actionClassName = actionClassName(this.model);
+ }
+ a.addClass(actionClassName);
}
if (this.options.action) {
this.delegateEvents({
diff --git a/dist/backbone.datagrid.min.js b/dist/backbone.datagrid.min.js
index 034bb5c..222c95d 100644
--- a/dist/backbone.datagrid.min.js
+++ b/dist/backbone.datagrid.min.js
@@ -2,4 +2,4 @@
//
// Copyright (c) 2012 Loïc Frering
// Distributed under the MIT license
-!function(){var a=Backbone.View.extend({addNestedView:function(a){this.nestedViews||(this.nestedViews=[]),this.nestedViews.push(a)},removeNestedViews:function(){this.nestedViews&&(_.invoke(this.nestedViews,"remove"),this.nestedViews=[])},remove:function(){return a.__super__.remove.call(this),this.removeNestedViews(),this}}),b=a.extend({initialize:function(a){this.options=a,this.columns=this.options.columns,_.defaults(this.options,{paginated:!1,page:1,perPage:10,tableClassName:"table",emptyMessage:"No results found.
"}),this.options.paginated&&!this.options.footerControls&&(this.options.footerControls={middle:h}),this.listenTo(this.collection,"add remove",this.render),this._prepare()},render:function(){return this.$el.empty(),this.removeNestedViews(),this.renderHeader(),this.renderTable(),this.renderFooter(),this},renderHeader:function(){if(this.options.headerControls){var a=_.extend({pager:this.pager},this.options.headerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderTable:function(){var a=new c({collection:this.collection,columns:this.columns,pager:this.pager,sorter:this.sorter,emptyMessage:this.options.emptyMessage,className:this.options.tableClassName,rowClassName:this.options.rowClassName,rowAttrs:this.options.rowAttrs,attributes:this.options.tableAttrs});this.$el.append(a.render().el),this.addNestedView(a)},renderFooter:function(){if(this.options.footerControls){var a=_.extend({pager:this.pager},this.options.footerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderPagination:function(){var a=new h({pager:this.pager});this.$el.append(a.render().el),this.addNestedView(a)},refresh:function(a){this.options.paginated?this._page(a):this.options.inMemory?(this.collection.trigger("reset",this.collection),a&&a.success&&a.success()):this._request(a)},sort:function(a,b){this.sorter.sort(a,b)},page:function(a){this.pager.page(a)},perPage:function(a){this.pager.set("perPage",a)},remove:function(){return b.__super__.remove.call(this),this.pager.off(),this},_sort:function(){this.options.inMemory?this._sortInMemory():this._sortRequest()},_sortInMemory:function(){this.options.paginated?(this._originalCollection.comparator=_.bind(this._comparator,this),this._originalCollection.sort(),this.page(1)):(this.collection.comparator=_.bind(this._comparator,this),this.collection.sort())},_comparator:function(a,b){var c=this._comparatorForColumn(this.sorter.get("column")),d=c(a,b);return this.sorter.sortedASC()?d:-d},_comparatorForColumn:function(a){var b=_.find(this.columns,function(b){return b.property===a||b.index===a});return b?b.comparator:void 0},_sortRequest:function(){this._request()},_page:function(a){this.options.inMemory?this._pageInMemory(a):this._pageRequest(a)},_pageRequest:function(a){this._request(a)},_request:function(a){a=a||{};var b=a.success,c=a.silent,d=a.data||{};a.data=_.extend(d,this._getRequestData()),a.success=_.bind(function(a){(!this.columns||_.isEmpty(this.columns))&&this._prepareColumns(),b&&b(),this.options.paginated&&this.pager.update(a),c||a.trigger("reset",a)},this),a.silent=!0,a.reset=!0,this.collection.fetch(a)},_getRequestData:function(){if(this.collection.data&&_.isFunction(this.collection.data))return this.collection.data(this.pager,this.sorter);if(this.collection.data&&"object"==typeof this.collection.data){var a={};return _.each(this.collection.data,function(b,c){_.isFunction(b)&&(b=b(this.pager,this.sorter)),a[c]=b},this),a}return this.options.paginated?{page:this.pager.get("currentPage"),per_page:this.pager.get("perPage")}:{}},_pageInMemory:function(a){this._originalCollection||(this._originalCollection=this.collection.clone());var b=this.pager.get("currentPage"),c=this.pager.get("perPage"),d=(b-1)*c,e=d+c;a&&a.success&&a.success(),this.pager.set("total",this._originalCollection.size()),this.collection.reset(this._originalCollection.slice(d,e),a)},_prepare:function(){this._prepareSorter(),this._preparePager(),this._prepareColumns(),this.refresh()},_prepareSorter:function(){this.sorter=new p,this.listenTo(this.sorter,"change",function(){this._sort(this.sorter.get("column"),this.sorter.get("order"))})},_preparePager:function(){this.pager=new o({currentPage:this.options.page,perPage:this.options.perPage}),this.listenTo(this.pager,"change:currentPage",function(){this._page()}),this.listenTo(this.pager,"change:perPage",function(){this.page(1)})},_prepareColumns:function(){!this.columns||_.isEmpty(this.columns)?this._defaultColumns():_.each(this.columns,function(a,b){this.columns[b]=this._prepareColumn(a,b)},this)},_prepareColumn:function(a,b){if(_.isString(a)&&(a={property:a}),_.isObject(a)){if(a.index=b,a.property)a.title=a.title||this._formatTitle(a.property);else if(!a.property&&!a.view)throw new Error("Column '"+a.title+"' has no property and must accordingly define a custom cell view.");if(this.options.inMemory&&a.sortable){if(!a.comparator&&!a.property&&!a.sortedProperty)throw new Error("Invalid column definition: a sortable column must have a comparator, property or sortedProperty defined.");a.comparator=a.comparator||this._defaultComparator(a.sortedProperty||a.property)}}return a},_formatTitle:function(a){return _.map(a.split(/\s|_/),function(a){return a.charAt(0).toUpperCase()+a.substr(1)}).join(" ")},_defaultColumns:function(){this.columns=[];var a=this.collection.first(),b=0;if(a)for(var c in a.toJSON())this.columns.push(this._prepareColumn(c,b++))},_defaultComparator:function(a){return function(b,c){var d=b.has(a)?b.get(a):"",e=c.has(a)?c.get(a):"";return d.localeCompare(e)}}}),c=b.Table=a.extend({tagName:"table",initialize:function(a){this.options=a,this.collection=this.options.collection,this.columns=this.options.columns,this.pager=this.options.pager,this.sorter=this.options.sorter,this.listenTo(this.collection,"reset",this.render)},render:function(){this.$el.empty(),this.removeNestedViews();var a=new d({columns:this.columns,sorter:this.sorter});return this.$el.append(a.render().el),this.addNestedView(a),this.$el.append(""),this.collection.isEmpty()?this.$el.append(this.options.emptyMessage):this.collection.forEach(this.renderRow,this),this},renderRow:function(a){var b={model:a,columns:this.columns,attributes:_.isFunction(this.options.rowAttrs)?this.options.rowAttrs(a):this.options.rowAttrs},c=this.options.rowClassName;_.isFunction(c)&&(c=c(a)),b.className=c;var d=new e(b);this.$("tbody").append(d.render(this.columns).el),this.addNestedView(d)}}),d=b.Header=a.extend({tagName:"thead",initialize:function(a){this.options=a,this.columns=this.options.columns,this.sorter=this.options.sorter},render:function(){this.removeNestedViews();var a,b=new Backbone.Model,c=[];_.each(this.columns,function(d){a=_.clone(d),a.property=d.property||d.index,a.view=d.headerView||{type:m,sorter:this.sorter},b.set(a.property,d.title),c.push(a)},this);var d=new e({model:b,columns:c,header:!0});return this.$el.html(d.render().el),this.addNestedView(d),this}}),e=b.Row=Backbone.View.extend({tagName:"tr",initialize:function(a){this.options=a,this.columns=this.options.columns,this.listenTo(this.model,"change",this.render)},render:function(){return this.$el.empty(),_.each(this.columns,this.renderCell,this),this},renderCell:function(a){var b=this._resolveCellView(a);this.$el.append(b.render().el)},_resolveCellView:function(a){var b={model:this.model,column:a,attributes:_.isFunction(a.cellAttrs)?a.cellAttrs(this.model):a.cellAttrs};(this.options.header||a.header)&&(b.tagName="th");var c=a.cellClassName;_.isFunction(c)&&(c=c(this.model)),b.className=c;var d=a.view||j;if("object"==typeof d||d.prototype&&d.prototype.render){if("object"==typeof d&&(_.extend(b,d),d=d.type,!d||!d.prototype||!d.prototype.render))throw new TypeError('Invalid view passed to column "'+a.title+'".')}else if(_.isString(d))b.callback=_.template(d),d=n;else{if(!_.isFunction(d)||d.prototype.render)throw new TypeError('Invalid view passed to column "'+a.title+'".');b.callback=d,d=k}return new d(b)}}),f=b.Controls=a.extend({initialize:function(a){this.options=a,this.pager=this.options.pager,this.left=this._resolveView(this.options.left),this.middle=this._resolveView(this.options.middle),this.right=this._resolveView(this.options.right)},render:function(){return this.$el.empty(),this.removeNestedViews(),_.chain(["left","middle","right"]).filter(function(a){return this[a]},this).each(function(a){var b=this[a];$("",{"class":"control "+a}).append(b.render().el).appendTo(this.$el),this.addNestedView(b)},this),this},_resolveView:function(a){if(!a)return null;var b;if(a.prototype&&a.prototype.render)b=a,a={};else{if("object"!=typeof a)throw new TypeError("Invalid view passed to controls.");if(b=a.control,!b||!b.prototype||!b.prototype.render)throw new TypeError("Invalid view passed to controls.")}return _.extend(a,{pager:this.pager}),new b(a)}}),g=b.Control=Backbone.View.extend({initialize:function(a){this.options=a,this.pager=this.options.pager}}),h=b.Pagination=g.extend({className:"pagination pagination-centered",events:{"click li:not(.disabled) a":"page","click li.disabled a":function(a){a.preventDefault()}},initialize:function(){h.__super__.initialize.apply(this,arguments),_.defaults(this.options,{full:!0}),this.listenTo(this.pager,"change",this.render)},render:function(){var a,b=$("");if(a=$('«'),this.pager.hasPrev()||a.addClass("disabled"),b.append(a),this.options.full&&this.pager.hasTotal())for(var c=1;c<=this.pager.get("totalPages");c++)a=$(""),c===this.pager.get("currentPage")&&a.addClass("active"),a.append(''+c+""),b.append(a);return a=$('»'),this.pager.hasNext()||a.addClass("disabled"),b.append(a),this.$el.html(b),this},page:function(a){var b=$(a.target);return b.parent().hasClass("prev")?this.pager.prev():b.parent().hasClass("next")?this.pager.next():this.pager.page(parseInt($(a.target).html(),10)),!1}}),i=b.ItemsPerPage=g.extend({events:{change:"perPage"},initialize:function(){i.__super__.initialize.apply(this,arguments),_.defaults(this.options,{increment:this.pager.get("perPage"),max:4*this.pager.get("perPage")})},render:function(){var a,b=$(""),c=this.options.increment,d=this.options.max;for(a=c;d>=a;a+=c)$option=$(""),$option.html(a),b.append($option);return this.$el.html(b),this},perPage:function(a){var b=$(a.target).val();this.pager.set("perPage",b)}}),j=b.Cell=Backbone.View.extend({tagName:"td",initialize:function(a){this.options=a,this.column=this.options.column},render:function(){return this._prepareValue(),this.$el.html(this.value),this},_prepareValue:function(){this.value=this.model.get(this.column.property)}}),k=b.CallbackCell=j.extend({initialize:function(){k.__super__.initialize.apply(this,arguments),this.callback=this.options.callback},_prepareValue:function(){this.value=this.callback(this.model)}}),l=b.ActionCell=j.extend({initialize:function(){l.__super__.initialize.apply(this,arguments)},action:function(){return this.options.action(this.model)},_prepareValue:function(){var a=$("");a.html(this.options.label),a.attr("href",this.options.href||"#"),this.options.actionClassName&&a.addClass(this.options.actionClassName),this.options.action&&this.delegateEvents({"click a":this.action}),this.value=a}}),m=b.HeaderCell=j.extend({initialize:function(){m.__super__.initialize.apply(this,arguments),this.sorter=this.options.sorter,this.column.sortable&&this.delegateEvents({click:"sort"})},render:function(){this._prepareValue();var a,b=this.value;return this.column.sortable&&(this.$el.addClass("sortable"),a=this.sorter.sortedBy(this.column.sortedProperty||this.column.property)||this.sorter.sortedBy(this.column.index)?this.sorter.sortedASC()?"icon-chevron-up":"icon-chevron-down":"icon-minus",b+=' '),this.$el.html(b),this},sort:function(){this.sorter.sort(this.column.sortedProperty||this.column.property)}}),n=b.TemplateCell=k.extend({_prepareValue:function(){this.value=this.callback(this.model.toJSON())}}),o=b.Pager=Backbone.Model.extend({initialize:function(){this.on("change:perPage change:total",function(){this.totalPages(this.get("total"))},this),this.has("total")&&this.totalPages(this.get("total"))},update:function(a){_.each(["hasNext","hasPrev","total","totalPages","lastPage"],function(b){_.isUndefined(a[b])||this.set(b,a[b])},this)},totalPages:function(a){_.isNumber(a)?this.set("totalPages",Math.ceil(a/this.get("perPage"))):this.set("totalPages",void 0)},page:function(a){this.inBounds(a)&&(a===this.get("currentPage")?this.trigger("change:currentPage"):this.set("currentPage",a))},next:function(){this.page(this.get("currentPage")+1)},prev:function(){this.page(this.get("currentPage")-1)},hasTotal:function(){return this.has("totalPages")},hasNext:function(){return this.hasTotal()?this.get("currentPage")1},inBounds:function(a){return!this.hasTotal()||a>0&&a<=this.get("totalPages")},set:function(){for(var a=[],b=0;bNo results found.
"}),this.options.paginated&&!this.options.footerControls&&(this.options.footerControls={middle:h}),this.listenTo(this.collection,"add remove",this.render),this._prepare()},render:function(){return this.$el.empty(),this.removeNestedViews(),this.renderHeader(),this.renderTable(),this.renderFooter(),this},renderHeader:function(){if(this.options.headerControls){var a=_.extend({pager:this.pager},this.options.headerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderTable:function(){var a=new c({collection:this.collection,columns:this.columns,pager:this.pager,sorter:this.sorter,emptyMessage:this.options.emptyMessage,className:this.options.tableClassName,rowClassName:this.options.rowClassName,rowAttrs:this.options.rowAttrs,attributes:this.options.tableAttrs});this.$el.append(a.render().el),this.addNestedView(a)},renderFooter:function(){if(this.options.footerControls){var a=_.extend({pager:this.pager},this.options.footerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderPagination:function(){var a=new h({pager:this.pager});this.$el.append(a.render().el),this.addNestedView(a)},refresh:function(a){this.options.paginated?this._page(a):this.options.inMemory?(this.collection.trigger("reset",this.collection),a&&a.success&&a.success()):this._request(a)},sort:function(a,b){this.sorter.sort(a,b)},page:function(a){this.pager.page(a)},perPage:function(a){this.pager.set("perPage",a)},remove:function(){return b.__super__.remove.call(this),this.pager.off(),this},_sort:function(){this.options.inMemory?this._sortInMemory():this._sortRequest()},_sortInMemory:function(){this.options.paginated?(this._originalCollection.comparator=_.bind(this._comparator,this),this._originalCollection.sort(),this.page(1)):(this.collection.comparator=_.bind(this._comparator,this),this.collection.sort())},_comparator:function(a,b){var c=this._comparatorForColumn(this.sorter.get("column")),d=c(a,b);return this.sorter.sortedASC()?d:-d},_comparatorForColumn:function(a){var b=_.find(this.columns,function(b){return b.property===a||b.index===a});return b?b.comparator:void 0},_sortRequest:function(){this._request()},_page:function(a){this.options.inMemory?this._pageInMemory(a):this._pageRequest(a)},_pageRequest:function(a){this._request(a)},_request:function(a){a=a||{};var b=a.success,c=a.silent,d=a.data||{};a.data=_.extend(d,this._getRequestData()),a.success=_.bind(function(a){(!this.columns||_.isEmpty(this.columns))&&this._prepareColumns(),b&&b(),this.options.paginated&&this.pager.update(a),c||a.trigger("reset",a)},this),a.silent=!0,a.reset=!0,this.collection.fetch(a)},_getRequestData:function(){if(this.collection.data&&_.isFunction(this.collection.data))return this.collection.data(this.pager,this.sorter);if(this.collection.data&&"object"==typeof this.collection.data){var a={};return _.each(this.collection.data,function(b,c){_.isFunction(b)&&(b=b(this.pager,this.sorter)),a[c]=b},this),a}return this.options.paginated?{page:this.pager.get("currentPage"),per_page:this.pager.get("perPage")}:{}},_pageInMemory:function(a){this._originalCollection||(this._originalCollection=this.collection.clone());var b=this.pager.get("currentPage"),c=this.pager.get("perPage"),d=(b-1)*c,e=d+c;a&&a.success&&a.success(),this.pager.set("total",this._originalCollection.size()),this.collection.reset(this._originalCollection.slice(d,e),a)},_prepare:function(){this._prepareSorter(),this._preparePager(),this._prepareColumns(),this.refresh()},_prepareSorter:function(){this.sorter=new p,this.listenTo(this.sorter,"change",function(){this._sort(this.sorter.get("column"),this.sorter.get("order"))})},_preparePager:function(){this.pager=new o({currentPage:this.options.page,perPage:this.options.perPage}),this.listenTo(this.pager,"change:currentPage",function(){this._page()}),this.listenTo(this.pager,"change:perPage",function(){this.page(1)})},_prepareColumns:function(){!this.columns||_.isEmpty(this.columns)?this._defaultColumns():_.each(this.columns,function(a,b){this.columns[b]=this._prepareColumn(a,b)},this)},_prepareColumn:function(a,b){if(_.isString(a)&&(a={property:a}),_.isObject(a)){if(a.index=b,a.property)a.title=a.title||this._formatTitle(a.property);else if(!a.property&&!a.view)throw new Error("Column '"+a.title+"' has no property and must accordingly define a custom cell view.");if(this.options.inMemory&&a.sortable){if(!a.comparator&&!a.property&&!a.sortedProperty)throw new Error("Invalid column definition: a sortable column must have a comparator, property or sortedProperty defined.");a.comparator=a.comparator||this._defaultComparator(a.sortedProperty||a.property)}}return a},_formatTitle:function(a){return _.map(a.split(/\s|_/),function(a){return a.charAt(0).toUpperCase()+a.substr(1)}).join(" ")},_defaultColumns:function(){this.columns=[];var a=this.collection.first(),b=0;if(a)for(var c in a.toJSON())this.columns.push(this._prepareColumn(c,b++))},_defaultComparator:function(a){return function(b,c){var d=b.has(a)?b.get(a):"",e=c.has(a)?c.get(a):"";return d.localeCompare(e)}}}),c=b.Table=a.extend({tagName:"table",initialize:function(a){this.options=a,this.collection=this.options.collection,this.columns=this.options.columns,this.pager=this.options.pager,this.sorter=this.options.sorter,this.listenTo(this.collection,"reset sort",this.render)},render:function(){this.$el.empty(),this.removeNestedViews();var a=new d({columns:this.columns,sorter:this.sorter});return this.$el.append(a.render().el),this.addNestedView(a),this.$el.append(""),this.collection.isEmpty()?this.$el.append(this.options.emptyMessage):this.collection.forEach(this.renderRow,this),this},renderRow:function(a){var b={model:a,columns:this.columns,attributes:_.isFunction(this.options.rowAttrs)?this.options.rowAttrs(a):this.options.rowAttrs},c=this.options.rowClassName;_.isFunction(c)&&(c=c(a)),b.className=c;var d=new e(b);this.$("tbody").append(d.render(this.columns).el),this.addNestedView(d)}}),d=b.Header=a.extend({tagName:"thead",initialize:function(a){this.options=a,this.columns=this.options.columns,this.sorter=this.options.sorter},render:function(){this.removeNestedViews();var a,b=new Backbone.Model,c=[];_.each(this.columns,function(d){a=_.clone(d),a.property=d.property||d.index,a.view=d.headerView||{type:m,sorter:this.sorter},b.set(a.property,d.title),c.push(a)},this);var d=new e({model:b,columns:c,header:!0});return this.$el.html(d.render().el),this.addNestedView(d),this}}),e=b.Row=Backbone.View.extend({tagName:"tr",initialize:function(a){this.options=a,this.columns=this.options.columns,this.listenTo(this.model,"change",this.render)},render:function(){return this.$el.empty(),_.each(this.columns,this.renderCell,this),this},renderCell:function(a){var b=this._resolveCellView(a);this.$el.append(b.render().el)},_resolveCellView:function(a){var b={model:this.model,column:a,attributes:_.isFunction(a.cellAttrs)?a.cellAttrs(this.model):a.cellAttrs};(this.options.header||a.header)&&(b.tagName="th");var c=a.cellClassName;_.isFunction(c)&&(c=c(this.model)),b.className=c;var d=a.view||j;if("object"==typeof d||d.prototype&&d.prototype.render){if("object"==typeof d&&(_.extend(b,d),d=d.type,!d||!d.prototype||!d.prototype.render))throw new TypeError('Invalid view passed to column "'+a.title+'".')}else if(_.isString(d))b.callback=_.template(d),d=n;else{if(!_.isFunction(d)||d.prototype.render)throw new TypeError('Invalid view passed to column "'+a.title+'".');b.callback=d,d=k}return new d(b)}}),f=b.Controls=a.extend({initialize:function(a){this.options=a,this.pager=this.options.pager,this.left=this._resolveView(this.options.left),this.middle=this._resolveView(this.options.middle),this.right=this._resolveView(this.options.right)},render:function(){return this.$el.empty(),this.removeNestedViews(),_.chain(["left","middle","right"]).filter(function(a){return this[a]},this).each(function(a){var b=this[a];$("",{"class":"control "+a}).append(b.render().el).appendTo(this.$el),this.addNestedView(b)},this),this},_resolveView:function(a){if(!a)return null;var b;if(a.prototype&&a.prototype.render)b=a,a={};else{if("object"!=typeof a)throw new TypeError("Invalid view passed to controls.");if(b=a.control,!b||!b.prototype||!b.prototype.render)throw new TypeError("Invalid view passed to controls.")}return _.extend(a,{pager:this.pager}),new b(a)}}),g=b.Control=Backbone.View.extend({initialize:function(a){this.options=a,this.pager=this.options.pager}}),h=b.Pagination=g.extend({className:"pagination pagination-centered",events:{"click li:not(.disabled) a":"page","click li.disabled a":function(a){a.preventDefault()}},initialize:function(){h.__super__.initialize.apply(this,arguments),_.defaults(this.options,{full:!0}),this.listenTo(this.pager,"change",this.render)},render:function(){var a,b=$("");if(a=$('«'),this.pager.hasPrev()||a.addClass("disabled"),b.append(a),this.options.full&&this.pager.hasTotal())for(var c=1;c<=this.pager.get("totalPages");c++)a=$(""),c===this.pager.get("currentPage")&&a.addClass("active"),a.append(''+c+""),b.append(a);return a=$('»'),this.pager.hasNext()||a.addClass("disabled"),b.append(a),this.$el.html(b),this},page:function(a){var b=$(a.target);return b.parent().hasClass("prev")?this.pager.prev():b.parent().hasClass("next")?this.pager.next():this.pager.page(parseInt($(a.target).html(),10)),!1}}),i=b.ItemsPerPage=g.extend({events:{change:"perPage"},initialize:function(){i.__super__.initialize.apply(this,arguments),_.defaults(this.options,{increment:this.pager.get("perPage"),max:4*this.pager.get("perPage")})},render:function(){var a,b=$(""),c=this.options.increment,d=this.options.max;for(a=c;d>=a;a+=c)$option=$(""),$option.html(a),b.append($option);return this.$el.html(b),this},perPage:function(a){var b=$(a.target).val();this.pager.set("perPage",b)}}),j=b.Cell=Backbone.View.extend({tagName:"td",initialize:function(a){this.options=a,this.column=this.options.column},render:function(){return this._prepareValue(),this.$el.html(this.value),this},_prepareValue:function(){this.value=this.model.get(this.column.property)}}),k=b.CallbackCell=j.extend({initialize:function(){k.__super__.initialize.apply(this,arguments),this.callback=this.options.callback},_prepareValue:function(){this.value=this.callback(this.model)}}),l=b.ActionCell=j.extend({initialize:function(){l.__super__.initialize.apply(this,arguments)},action:function(){return this.options.action(this.model)},_prepareValue:function(){var a=$("");if(a.html(this.options.label),a.attr("href",this.options.href||"#"),this.options.actionClassName){var b=this.options.actionClassName;_.isFunction(b)&&(b=b(this.model)),a.addClass(b)}this.options.action&&this.delegateEvents({"click a":this.action}),this.value=a}}),m=b.HeaderCell=j.extend({initialize:function(){m.__super__.initialize.apply(this,arguments),this.sorter=this.options.sorter,this.column.sortable&&this.delegateEvents({click:"sort"})},render:function(){this._prepareValue();var a,b=this.value;return this.column.sortable&&(this.$el.addClass("sortable"),a=this.sorter.sortedBy(this.column.sortedProperty||this.column.property)||this.sorter.sortedBy(this.column.index)?this.sorter.sortedASC()?"icon-chevron-up":"icon-chevron-down":"icon-minus",b+=' '),this.$el.html(b),this},sort:function(){this.sorter.sort(this.column.sortedProperty||this.column.property)}}),n=b.TemplateCell=k.extend({_prepareValue:function(){this.value=this.callback(this.model.toJSON())}}),o=b.Pager=Backbone.Model.extend({initialize:function(){this.on("change:perPage change:total",function(){this.totalPages(this.get("total"))},this),this.has("total")&&this.totalPages(this.get("total"))},update:function(a){_.each(["hasNext","hasPrev","total","totalPages","lastPage"],function(b){_.isUndefined(a[b])||this.set(b,a[b])},this)},totalPages:function(a){_.isNumber(a)?this.set("totalPages",Math.ceil(a/this.get("perPage"))):this.set("totalPages",void 0)},page:function(a){this.inBounds(a)&&(a===this.get("currentPage")?this.trigger("change:currentPage"):this.set("currentPage",a))},next:function(){this.page(this.get("currentPage")+1)},prev:function(){this.page(this.get("currentPage")-1)},hasTotal:function(){return this.has("totalPages")},hasNext:function(){return this.hasTotal()?this.get("currentPage")1},inBounds:function(a){return!this.hasTotal()||a>0&&a<=this.get("totalPages")},set:function(){for(var a=[],b=0;b
Date: Sat, 10 Aug 2013 22:19:17 +0300
Subject: [PATCH 2/4] Add a comment line for the fix for Issue #10.
---
dist/backbone.datagrid.js | 1 +
src/views/cells/action-cell.js | 1 +
2 files changed, 2 insertions(+)
diff --git a/dist/backbone.datagrid.js b/dist/backbone.datagrid.js
index 547688c..2c0a810 100644
--- a/dist/backbone.datagrid.js
+++ b/dist/backbone.datagrid.js
@@ -688,6 +688,7 @@ var ActionCell = Datagrid.ActionCell = Cell.extend({
a.attr('href', this.options.href || '#');
if (this.options.actionClassName) {
var actionClassName = this.options.actionClassName;
+ // Check if the classname is actually a callback and run it if it is.
if (_.isFunction(actionClassName)) {
actionClassName = actionClassName(this.model);
}
diff --git a/src/views/cells/action-cell.js b/src/views/cells/action-cell.js
index c9ab31c..76d036b 100644
--- a/src/views/cells/action-cell.js
+++ b/src/views/cells/action-cell.js
@@ -14,6 +14,7 @@ var ActionCell = Datagrid.ActionCell = Cell.extend({
a.attr('href', this.options.href || '#');
if (this.options.actionClassName) {
var actionClassName = this.options.actionClassName;
+ // Check if the classname is actually a callback and run it if it is.
if (_.isFunction(actionClassName)) {
actionClassName = actionClassName(this.model);
}
From 19ed1c289de6641927cdb1dda28a280dd16320a1 Mon Sep 17 00:00:00 2001
From: Gerasimos Tzoganis
Date: Thu, 15 Aug 2013 15:29:34 +0300
Subject: [PATCH 3/4] Revert "Refresh the table when a sort event is triggered
by the collection."
This reverts commit 5c53ee9d8b8d3dfdea8bc0a7fcd17b13d3bbadf4.
---
src/views/table.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/views/table.js b/src/views/table.js
index 7f2261d..af1ebc8 100644
--- a/src/views/table.js
+++ b/src/views/table.js
@@ -8,7 +8,7 @@ var Table = Datagrid.Table = ComposedView.extend({
this.pager = this.options.pager;
this.sorter = this.options.sorter;
- this.listenTo(this.collection, 'reset sort', this.render);
+ this.listenTo(this.collection, 'reset', this.render);
},
render: function() {
From 692b8760e2d155a6ed61aa9f05b3ac89d2a2334d Mon Sep 17 00:00:00 2001
From: Gerasimos Tzoganis
Date: Thu, 15 Aug 2013 15:49:52 +0300
Subject: [PATCH 4/4] Revert dist files
---
dist/backbone.datagrid.js | 9 ++-------
dist/backbone.datagrid.min.js | 2 +-
2 files changed, 3 insertions(+), 8 deletions(-)
diff --git a/dist/backbone.datagrid.js b/dist/backbone.datagrid.js
index 2c0a810..59ad8a2 100644
--- a/dist/backbone.datagrid.js
+++ b/dist/backbone.datagrid.js
@@ -344,7 +344,7 @@ var Table = Datagrid.Table = ComposedView.extend({
this.pager = this.options.pager;
this.sorter = this.options.sorter;
- this.listenTo(this.collection, 'reset sort', this.render);
+ this.listenTo(this.collection, 'reset', this.render);
},
render: function() {
@@ -687,12 +687,7 @@ var ActionCell = Datagrid.ActionCell = Cell.extend({
a.html(this.options.label);
a.attr('href', this.options.href || '#');
if (this.options.actionClassName) {
- var actionClassName = this.options.actionClassName;
- // Check if the classname is actually a callback and run it if it is.
- if (_.isFunction(actionClassName)) {
- actionClassName = actionClassName(this.model);
- }
- a.addClass(actionClassName);
+ a.addClass(this.options.actionClassName);
}
if (this.options.action) {
this.delegateEvents({
diff --git a/dist/backbone.datagrid.min.js b/dist/backbone.datagrid.min.js
index 222c95d..034bb5c 100644
--- a/dist/backbone.datagrid.min.js
+++ b/dist/backbone.datagrid.min.js
@@ -2,4 +2,4 @@
//
// Copyright (c) 2012 Loïc Frering
// Distributed under the MIT license
-!function(){var a=Backbone.View.extend({addNestedView:function(a){this.nestedViews||(this.nestedViews=[]),this.nestedViews.push(a)},removeNestedViews:function(){this.nestedViews&&(_.invoke(this.nestedViews,"remove"),this.nestedViews=[])},remove:function(){return a.__super__.remove.call(this),this.removeNestedViews(),this}}),b=a.extend({initialize:function(a){this.options=a,this.columns=this.options.columns,_.defaults(this.options,{paginated:!1,page:1,perPage:10,tableClassName:"table",emptyMessage:"No results found.
"}),this.options.paginated&&!this.options.footerControls&&(this.options.footerControls={middle:h}),this.listenTo(this.collection,"add remove",this.render),this._prepare()},render:function(){return this.$el.empty(),this.removeNestedViews(),this.renderHeader(),this.renderTable(),this.renderFooter(),this},renderHeader:function(){if(this.options.headerControls){var a=_.extend({pager:this.pager},this.options.headerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderTable:function(){var a=new c({collection:this.collection,columns:this.columns,pager:this.pager,sorter:this.sorter,emptyMessage:this.options.emptyMessage,className:this.options.tableClassName,rowClassName:this.options.rowClassName,rowAttrs:this.options.rowAttrs,attributes:this.options.tableAttrs});this.$el.append(a.render().el),this.addNestedView(a)},renderFooter:function(){if(this.options.footerControls){var a=_.extend({pager:this.pager},this.options.footerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderPagination:function(){var a=new h({pager:this.pager});this.$el.append(a.render().el),this.addNestedView(a)},refresh:function(a){this.options.paginated?this._page(a):this.options.inMemory?(this.collection.trigger("reset",this.collection),a&&a.success&&a.success()):this._request(a)},sort:function(a,b){this.sorter.sort(a,b)},page:function(a){this.pager.page(a)},perPage:function(a){this.pager.set("perPage",a)},remove:function(){return b.__super__.remove.call(this),this.pager.off(),this},_sort:function(){this.options.inMemory?this._sortInMemory():this._sortRequest()},_sortInMemory:function(){this.options.paginated?(this._originalCollection.comparator=_.bind(this._comparator,this),this._originalCollection.sort(),this.page(1)):(this.collection.comparator=_.bind(this._comparator,this),this.collection.sort())},_comparator:function(a,b){var c=this._comparatorForColumn(this.sorter.get("column")),d=c(a,b);return this.sorter.sortedASC()?d:-d},_comparatorForColumn:function(a){var b=_.find(this.columns,function(b){return b.property===a||b.index===a});return b?b.comparator:void 0},_sortRequest:function(){this._request()},_page:function(a){this.options.inMemory?this._pageInMemory(a):this._pageRequest(a)},_pageRequest:function(a){this._request(a)},_request:function(a){a=a||{};var b=a.success,c=a.silent,d=a.data||{};a.data=_.extend(d,this._getRequestData()),a.success=_.bind(function(a){(!this.columns||_.isEmpty(this.columns))&&this._prepareColumns(),b&&b(),this.options.paginated&&this.pager.update(a),c||a.trigger("reset",a)},this),a.silent=!0,a.reset=!0,this.collection.fetch(a)},_getRequestData:function(){if(this.collection.data&&_.isFunction(this.collection.data))return this.collection.data(this.pager,this.sorter);if(this.collection.data&&"object"==typeof this.collection.data){var a={};return _.each(this.collection.data,function(b,c){_.isFunction(b)&&(b=b(this.pager,this.sorter)),a[c]=b},this),a}return this.options.paginated?{page:this.pager.get("currentPage"),per_page:this.pager.get("perPage")}:{}},_pageInMemory:function(a){this._originalCollection||(this._originalCollection=this.collection.clone());var b=this.pager.get("currentPage"),c=this.pager.get("perPage"),d=(b-1)*c,e=d+c;a&&a.success&&a.success(),this.pager.set("total",this._originalCollection.size()),this.collection.reset(this._originalCollection.slice(d,e),a)},_prepare:function(){this._prepareSorter(),this._preparePager(),this._prepareColumns(),this.refresh()},_prepareSorter:function(){this.sorter=new p,this.listenTo(this.sorter,"change",function(){this._sort(this.sorter.get("column"),this.sorter.get("order"))})},_preparePager:function(){this.pager=new o({currentPage:this.options.page,perPage:this.options.perPage}),this.listenTo(this.pager,"change:currentPage",function(){this._page()}),this.listenTo(this.pager,"change:perPage",function(){this.page(1)})},_prepareColumns:function(){!this.columns||_.isEmpty(this.columns)?this._defaultColumns():_.each(this.columns,function(a,b){this.columns[b]=this._prepareColumn(a,b)},this)},_prepareColumn:function(a,b){if(_.isString(a)&&(a={property:a}),_.isObject(a)){if(a.index=b,a.property)a.title=a.title||this._formatTitle(a.property);else if(!a.property&&!a.view)throw new Error("Column '"+a.title+"' has no property and must accordingly define a custom cell view.");if(this.options.inMemory&&a.sortable){if(!a.comparator&&!a.property&&!a.sortedProperty)throw new Error("Invalid column definition: a sortable column must have a comparator, property or sortedProperty defined.");a.comparator=a.comparator||this._defaultComparator(a.sortedProperty||a.property)}}return a},_formatTitle:function(a){return _.map(a.split(/\s|_/),function(a){return a.charAt(0).toUpperCase()+a.substr(1)}).join(" ")},_defaultColumns:function(){this.columns=[];var a=this.collection.first(),b=0;if(a)for(var c in a.toJSON())this.columns.push(this._prepareColumn(c,b++))},_defaultComparator:function(a){return function(b,c){var d=b.has(a)?b.get(a):"",e=c.has(a)?c.get(a):"";return d.localeCompare(e)}}}),c=b.Table=a.extend({tagName:"table",initialize:function(a){this.options=a,this.collection=this.options.collection,this.columns=this.options.columns,this.pager=this.options.pager,this.sorter=this.options.sorter,this.listenTo(this.collection,"reset sort",this.render)},render:function(){this.$el.empty(),this.removeNestedViews();var a=new d({columns:this.columns,sorter:this.sorter});return this.$el.append(a.render().el),this.addNestedView(a),this.$el.append(""),this.collection.isEmpty()?this.$el.append(this.options.emptyMessage):this.collection.forEach(this.renderRow,this),this},renderRow:function(a){var b={model:a,columns:this.columns,attributes:_.isFunction(this.options.rowAttrs)?this.options.rowAttrs(a):this.options.rowAttrs},c=this.options.rowClassName;_.isFunction(c)&&(c=c(a)),b.className=c;var d=new e(b);this.$("tbody").append(d.render(this.columns).el),this.addNestedView(d)}}),d=b.Header=a.extend({tagName:"thead",initialize:function(a){this.options=a,this.columns=this.options.columns,this.sorter=this.options.sorter},render:function(){this.removeNestedViews();var a,b=new Backbone.Model,c=[];_.each(this.columns,function(d){a=_.clone(d),a.property=d.property||d.index,a.view=d.headerView||{type:m,sorter:this.sorter},b.set(a.property,d.title),c.push(a)},this);var d=new e({model:b,columns:c,header:!0});return this.$el.html(d.render().el),this.addNestedView(d),this}}),e=b.Row=Backbone.View.extend({tagName:"tr",initialize:function(a){this.options=a,this.columns=this.options.columns,this.listenTo(this.model,"change",this.render)},render:function(){return this.$el.empty(),_.each(this.columns,this.renderCell,this),this},renderCell:function(a){var b=this._resolveCellView(a);this.$el.append(b.render().el)},_resolveCellView:function(a){var b={model:this.model,column:a,attributes:_.isFunction(a.cellAttrs)?a.cellAttrs(this.model):a.cellAttrs};(this.options.header||a.header)&&(b.tagName="th");var c=a.cellClassName;_.isFunction(c)&&(c=c(this.model)),b.className=c;var d=a.view||j;if("object"==typeof d||d.prototype&&d.prototype.render){if("object"==typeof d&&(_.extend(b,d),d=d.type,!d||!d.prototype||!d.prototype.render))throw new TypeError('Invalid view passed to column "'+a.title+'".')}else if(_.isString(d))b.callback=_.template(d),d=n;else{if(!_.isFunction(d)||d.prototype.render)throw new TypeError('Invalid view passed to column "'+a.title+'".');b.callback=d,d=k}return new d(b)}}),f=b.Controls=a.extend({initialize:function(a){this.options=a,this.pager=this.options.pager,this.left=this._resolveView(this.options.left),this.middle=this._resolveView(this.options.middle),this.right=this._resolveView(this.options.right)},render:function(){return this.$el.empty(),this.removeNestedViews(),_.chain(["left","middle","right"]).filter(function(a){return this[a]},this).each(function(a){var b=this[a];$("",{"class":"control "+a}).append(b.render().el).appendTo(this.$el),this.addNestedView(b)},this),this},_resolveView:function(a){if(!a)return null;var b;if(a.prototype&&a.prototype.render)b=a,a={};else{if("object"!=typeof a)throw new TypeError("Invalid view passed to controls.");if(b=a.control,!b||!b.prototype||!b.prototype.render)throw new TypeError("Invalid view passed to controls.")}return _.extend(a,{pager:this.pager}),new b(a)}}),g=b.Control=Backbone.View.extend({initialize:function(a){this.options=a,this.pager=this.options.pager}}),h=b.Pagination=g.extend({className:"pagination pagination-centered",events:{"click li:not(.disabled) a":"page","click li.disabled a":function(a){a.preventDefault()}},initialize:function(){h.__super__.initialize.apply(this,arguments),_.defaults(this.options,{full:!0}),this.listenTo(this.pager,"change",this.render)},render:function(){var a,b=$("");if(a=$('«'),this.pager.hasPrev()||a.addClass("disabled"),b.append(a),this.options.full&&this.pager.hasTotal())for(var c=1;c<=this.pager.get("totalPages");c++)a=$(""),c===this.pager.get("currentPage")&&a.addClass("active"),a.append(''+c+""),b.append(a);return a=$('»'),this.pager.hasNext()||a.addClass("disabled"),b.append(a),this.$el.html(b),this},page:function(a){var b=$(a.target);return b.parent().hasClass("prev")?this.pager.prev():b.parent().hasClass("next")?this.pager.next():this.pager.page(parseInt($(a.target).html(),10)),!1}}),i=b.ItemsPerPage=g.extend({events:{change:"perPage"},initialize:function(){i.__super__.initialize.apply(this,arguments),_.defaults(this.options,{increment:this.pager.get("perPage"),max:4*this.pager.get("perPage")})},render:function(){var a,b=$(""),c=this.options.increment,d=this.options.max;for(a=c;d>=a;a+=c)$option=$(""),$option.html(a),b.append($option);return this.$el.html(b),this},perPage:function(a){var b=$(a.target).val();this.pager.set("perPage",b)}}),j=b.Cell=Backbone.View.extend({tagName:"td",initialize:function(a){this.options=a,this.column=this.options.column},render:function(){return this._prepareValue(),this.$el.html(this.value),this},_prepareValue:function(){this.value=this.model.get(this.column.property)}}),k=b.CallbackCell=j.extend({initialize:function(){k.__super__.initialize.apply(this,arguments),this.callback=this.options.callback},_prepareValue:function(){this.value=this.callback(this.model)}}),l=b.ActionCell=j.extend({initialize:function(){l.__super__.initialize.apply(this,arguments)},action:function(){return this.options.action(this.model)},_prepareValue:function(){var a=$("");if(a.html(this.options.label),a.attr("href",this.options.href||"#"),this.options.actionClassName){var b=this.options.actionClassName;_.isFunction(b)&&(b=b(this.model)),a.addClass(b)}this.options.action&&this.delegateEvents({"click a":this.action}),this.value=a}}),m=b.HeaderCell=j.extend({initialize:function(){m.__super__.initialize.apply(this,arguments),this.sorter=this.options.sorter,this.column.sortable&&this.delegateEvents({click:"sort"})},render:function(){this._prepareValue();var a,b=this.value;return this.column.sortable&&(this.$el.addClass("sortable"),a=this.sorter.sortedBy(this.column.sortedProperty||this.column.property)||this.sorter.sortedBy(this.column.index)?this.sorter.sortedASC()?"icon-chevron-up":"icon-chevron-down":"icon-minus",b+=' '),this.$el.html(b),this},sort:function(){this.sorter.sort(this.column.sortedProperty||this.column.property)}}),n=b.TemplateCell=k.extend({_prepareValue:function(){this.value=this.callback(this.model.toJSON())}}),o=b.Pager=Backbone.Model.extend({initialize:function(){this.on("change:perPage change:total",function(){this.totalPages(this.get("total"))},this),this.has("total")&&this.totalPages(this.get("total"))},update:function(a){_.each(["hasNext","hasPrev","total","totalPages","lastPage"],function(b){_.isUndefined(a[b])||this.set(b,a[b])},this)},totalPages:function(a){_.isNumber(a)?this.set("totalPages",Math.ceil(a/this.get("perPage"))):this.set("totalPages",void 0)},page:function(a){this.inBounds(a)&&(a===this.get("currentPage")?this.trigger("change:currentPage"):this.set("currentPage",a))},next:function(){this.page(this.get("currentPage")+1)},prev:function(){this.page(this.get("currentPage")-1)},hasTotal:function(){return this.has("totalPages")},hasNext:function(){return this.hasTotal()?this.get("currentPage")1},inBounds:function(a){return!this.hasTotal()||a>0&&a<=this.get("totalPages")},set:function(){for(var a=[],b=0;bNo results found."}),this.options.paginated&&!this.options.footerControls&&(this.options.footerControls={middle:h}),this.listenTo(this.collection,"add remove",this.render),this._prepare()},render:function(){return this.$el.empty(),this.removeNestedViews(),this.renderHeader(),this.renderTable(),this.renderFooter(),this},renderHeader:function(){if(this.options.headerControls){var a=_.extend({pager:this.pager},this.options.headerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderTable:function(){var a=new c({collection:this.collection,columns:this.columns,pager:this.pager,sorter:this.sorter,emptyMessage:this.options.emptyMessage,className:this.options.tableClassName,rowClassName:this.options.rowClassName,rowAttrs:this.options.rowAttrs,attributes:this.options.tableAttrs});this.$el.append(a.render().el),this.addNestedView(a)},renderFooter:function(){if(this.options.footerControls){var a=_.extend({pager:this.pager},this.options.footerControls),b=new f(a);this.$el.append(b.render().el),this.addNestedView(b)}},renderPagination:function(){var a=new h({pager:this.pager});this.$el.append(a.render().el),this.addNestedView(a)},refresh:function(a){this.options.paginated?this._page(a):this.options.inMemory?(this.collection.trigger("reset",this.collection),a&&a.success&&a.success()):this._request(a)},sort:function(a,b){this.sorter.sort(a,b)},page:function(a){this.pager.page(a)},perPage:function(a){this.pager.set("perPage",a)},remove:function(){return b.__super__.remove.call(this),this.pager.off(),this},_sort:function(){this.options.inMemory?this._sortInMemory():this._sortRequest()},_sortInMemory:function(){this.options.paginated?(this._originalCollection.comparator=_.bind(this._comparator,this),this._originalCollection.sort(),this.page(1)):(this.collection.comparator=_.bind(this._comparator,this),this.collection.sort())},_comparator:function(a,b){var c=this._comparatorForColumn(this.sorter.get("column")),d=c(a,b);return this.sorter.sortedASC()?d:-d},_comparatorForColumn:function(a){var b=_.find(this.columns,function(b){return b.property===a||b.index===a});return b?b.comparator:void 0},_sortRequest:function(){this._request()},_page:function(a){this.options.inMemory?this._pageInMemory(a):this._pageRequest(a)},_pageRequest:function(a){this._request(a)},_request:function(a){a=a||{};var b=a.success,c=a.silent,d=a.data||{};a.data=_.extend(d,this._getRequestData()),a.success=_.bind(function(a){(!this.columns||_.isEmpty(this.columns))&&this._prepareColumns(),b&&b(),this.options.paginated&&this.pager.update(a),c||a.trigger("reset",a)},this),a.silent=!0,a.reset=!0,this.collection.fetch(a)},_getRequestData:function(){if(this.collection.data&&_.isFunction(this.collection.data))return this.collection.data(this.pager,this.sorter);if(this.collection.data&&"object"==typeof this.collection.data){var a={};return _.each(this.collection.data,function(b,c){_.isFunction(b)&&(b=b(this.pager,this.sorter)),a[c]=b},this),a}return this.options.paginated?{page:this.pager.get("currentPage"),per_page:this.pager.get("perPage")}:{}},_pageInMemory:function(a){this._originalCollection||(this._originalCollection=this.collection.clone());var b=this.pager.get("currentPage"),c=this.pager.get("perPage"),d=(b-1)*c,e=d+c;a&&a.success&&a.success(),this.pager.set("total",this._originalCollection.size()),this.collection.reset(this._originalCollection.slice(d,e),a)},_prepare:function(){this._prepareSorter(),this._preparePager(),this._prepareColumns(),this.refresh()},_prepareSorter:function(){this.sorter=new p,this.listenTo(this.sorter,"change",function(){this._sort(this.sorter.get("column"),this.sorter.get("order"))})},_preparePager:function(){this.pager=new o({currentPage:this.options.page,perPage:this.options.perPage}),this.listenTo(this.pager,"change:currentPage",function(){this._page()}),this.listenTo(this.pager,"change:perPage",function(){this.page(1)})},_prepareColumns:function(){!this.columns||_.isEmpty(this.columns)?this._defaultColumns():_.each(this.columns,function(a,b){this.columns[b]=this._prepareColumn(a,b)},this)},_prepareColumn:function(a,b){if(_.isString(a)&&(a={property:a}),_.isObject(a)){if(a.index=b,a.property)a.title=a.title||this._formatTitle(a.property);else if(!a.property&&!a.view)throw new Error("Column '"+a.title+"' has no property and must accordingly define a custom cell view.");if(this.options.inMemory&&a.sortable){if(!a.comparator&&!a.property&&!a.sortedProperty)throw new Error("Invalid column definition: a sortable column must have a comparator, property or sortedProperty defined.");a.comparator=a.comparator||this._defaultComparator(a.sortedProperty||a.property)}}return a},_formatTitle:function(a){return _.map(a.split(/\s|_/),function(a){return a.charAt(0).toUpperCase()+a.substr(1)}).join(" ")},_defaultColumns:function(){this.columns=[];var a=this.collection.first(),b=0;if(a)for(var c in a.toJSON())this.columns.push(this._prepareColumn(c,b++))},_defaultComparator:function(a){return function(b,c){var d=b.has(a)?b.get(a):"",e=c.has(a)?c.get(a):"";return d.localeCompare(e)}}}),c=b.Table=a.extend({tagName:"table",initialize:function(a){this.options=a,this.collection=this.options.collection,this.columns=this.options.columns,this.pager=this.options.pager,this.sorter=this.options.sorter,this.listenTo(this.collection,"reset",this.render)},render:function(){this.$el.empty(),this.removeNestedViews();var a=new d({columns:this.columns,sorter:this.sorter});return this.$el.append(a.render().el),this.addNestedView(a),this.$el.append(""),this.collection.isEmpty()?this.$el.append(this.options.emptyMessage):this.collection.forEach(this.renderRow,this),this},renderRow:function(a){var b={model:a,columns:this.columns,attributes:_.isFunction(this.options.rowAttrs)?this.options.rowAttrs(a):this.options.rowAttrs},c=this.options.rowClassName;_.isFunction(c)&&(c=c(a)),b.className=c;var d=new e(b);this.$("tbody").append(d.render(this.columns).el),this.addNestedView(d)}}),d=b.Header=a.extend({tagName:"thead",initialize:function(a){this.options=a,this.columns=this.options.columns,this.sorter=this.options.sorter},render:function(){this.removeNestedViews();var a,b=new Backbone.Model,c=[];_.each(this.columns,function(d){a=_.clone(d),a.property=d.property||d.index,a.view=d.headerView||{type:m,sorter:this.sorter},b.set(a.property,d.title),c.push(a)},this);var d=new e({model:b,columns:c,header:!0});return this.$el.html(d.render().el),this.addNestedView(d),this}}),e=b.Row=Backbone.View.extend({tagName:"tr",initialize:function(a){this.options=a,this.columns=this.options.columns,this.listenTo(this.model,"change",this.render)},render:function(){return this.$el.empty(),_.each(this.columns,this.renderCell,this),this},renderCell:function(a){var b=this._resolveCellView(a);this.$el.append(b.render().el)},_resolveCellView:function(a){var b={model:this.model,column:a,attributes:_.isFunction(a.cellAttrs)?a.cellAttrs(this.model):a.cellAttrs};(this.options.header||a.header)&&(b.tagName="th");var c=a.cellClassName;_.isFunction(c)&&(c=c(this.model)),b.className=c;var d=a.view||j;if("object"==typeof d||d.prototype&&d.prototype.render){if("object"==typeof d&&(_.extend(b,d),d=d.type,!d||!d.prototype||!d.prototype.render))throw new TypeError('Invalid view passed to column "'+a.title+'".')}else if(_.isString(d))b.callback=_.template(d),d=n;else{if(!_.isFunction(d)||d.prototype.render)throw new TypeError('Invalid view passed to column "'+a.title+'".');b.callback=d,d=k}return new d(b)}}),f=b.Controls=a.extend({initialize:function(a){this.options=a,this.pager=this.options.pager,this.left=this._resolveView(this.options.left),this.middle=this._resolveView(this.options.middle),this.right=this._resolveView(this.options.right)},render:function(){return this.$el.empty(),this.removeNestedViews(),_.chain(["left","middle","right"]).filter(function(a){return this[a]},this).each(function(a){var b=this[a];$("",{"class":"control "+a}).append(b.render().el).appendTo(this.$el),this.addNestedView(b)},this),this},_resolveView:function(a){if(!a)return null;var b;if(a.prototype&&a.prototype.render)b=a,a={};else{if("object"!=typeof a)throw new TypeError("Invalid view passed to controls.");if(b=a.control,!b||!b.prototype||!b.prototype.render)throw new TypeError("Invalid view passed to controls.")}return _.extend(a,{pager:this.pager}),new b(a)}}),g=b.Control=Backbone.View.extend({initialize:function(a){this.options=a,this.pager=this.options.pager}}),h=b.Pagination=g.extend({className:"pagination pagination-centered",events:{"click li:not(.disabled) a":"page","click li.disabled a":function(a){a.preventDefault()}},initialize:function(){h.__super__.initialize.apply(this,arguments),_.defaults(this.options,{full:!0}),this.listenTo(this.pager,"change",this.render)},render:function(){var a,b=$("");if(a=$('«'),this.pager.hasPrev()||a.addClass("disabled"),b.append(a),this.options.full&&this.pager.hasTotal())for(var c=1;c<=this.pager.get("totalPages");c++)a=$(""),c===this.pager.get("currentPage")&&a.addClass("active"),a.append(''+c+""),b.append(a);return a=$('»'),this.pager.hasNext()||a.addClass("disabled"),b.append(a),this.$el.html(b),this},page:function(a){var b=$(a.target);return b.parent().hasClass("prev")?this.pager.prev():b.parent().hasClass("next")?this.pager.next():this.pager.page(parseInt($(a.target).html(),10)),!1}}),i=b.ItemsPerPage=g.extend({events:{change:"perPage"},initialize:function(){i.__super__.initialize.apply(this,arguments),_.defaults(this.options,{increment:this.pager.get("perPage"),max:4*this.pager.get("perPage")})},render:function(){var a,b=$(""),c=this.options.increment,d=this.options.max;for(a=c;d>=a;a+=c)$option=$(""),$option.html(a),b.append($option);return this.$el.html(b),this},perPage:function(a){var b=$(a.target).val();this.pager.set("perPage",b)}}),j=b.Cell=Backbone.View.extend({tagName:"td",initialize:function(a){this.options=a,this.column=this.options.column},render:function(){return this._prepareValue(),this.$el.html(this.value),this},_prepareValue:function(){this.value=this.model.get(this.column.property)}}),k=b.CallbackCell=j.extend({initialize:function(){k.__super__.initialize.apply(this,arguments),this.callback=this.options.callback},_prepareValue:function(){this.value=this.callback(this.model)}}),l=b.ActionCell=j.extend({initialize:function(){l.__super__.initialize.apply(this,arguments)},action:function(){return this.options.action(this.model)},_prepareValue:function(){var a=$("");a.html(this.options.label),a.attr("href",this.options.href||"#"),this.options.actionClassName&&a.addClass(this.options.actionClassName),this.options.action&&this.delegateEvents({"click a":this.action}),this.value=a}}),m=b.HeaderCell=j.extend({initialize:function(){m.__super__.initialize.apply(this,arguments),this.sorter=this.options.sorter,this.column.sortable&&this.delegateEvents({click:"sort"})},render:function(){this._prepareValue();var a,b=this.value;return this.column.sortable&&(this.$el.addClass("sortable"),a=this.sorter.sortedBy(this.column.sortedProperty||this.column.property)||this.sorter.sortedBy(this.column.index)?this.sorter.sortedASC()?"icon-chevron-up":"icon-chevron-down":"icon-minus",b+=' '),this.$el.html(b),this},sort:function(){this.sorter.sort(this.column.sortedProperty||this.column.property)}}),n=b.TemplateCell=k.extend({_prepareValue:function(){this.value=this.callback(this.model.toJSON())}}),o=b.Pager=Backbone.Model.extend({initialize:function(){this.on("change:perPage change:total",function(){this.totalPages(this.get("total"))},this),this.has("total")&&this.totalPages(this.get("total"))},update:function(a){_.each(["hasNext","hasPrev","total","totalPages","lastPage"],function(b){_.isUndefined(a[b])||this.set(b,a[b])},this)},totalPages:function(a){_.isNumber(a)?this.set("totalPages",Math.ceil(a/this.get("perPage"))):this.set("totalPages",void 0)},page:function(a){this.inBounds(a)&&(a===this.get("currentPage")?this.trigger("change:currentPage"):this.set("currentPage",a))},next:function(){this.page(this.get("currentPage")+1)},prev:function(){this.page(this.get("currentPage")-1)},hasTotal:function(){return this.has("totalPages")},hasNext:function(){return this.hasTotal()?this.get("currentPage")1},inBounds:function(a){return!this.hasTotal()||a>0&&a<=this.get("totalPages")},set:function(){for(var a=[],b=0;b