Skip to content

Commit

Permalink
some js refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch09 committed Jun 28, 2015
1 parent 4a4c0ef commit 13c453d
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions app/assets/javascripts/tabulatr/_tabulatr.js
Expand Up @@ -9,6 +9,8 @@ function Tabulatr(id){
this.hasInfiniteScrolling = false;
}

var tabulatr_tables;

var cbfn = function(event, isInView, visiblePartX, visiblePartY) {
if (isInView && visiblePartY !== 'top' && visiblePartY !== 'bottom') {
var tableId = $(event.currentTarget).data('table');
Expand All @@ -22,7 +24,6 @@ var cbfn = function(event, isInView, visiblePartX, visiblePartY) {
}
};

var tabulatr_tables;
Tabulatr.prototype = {
constructor: Tabulatr,

Expand Down Expand Up @@ -98,30 +99,36 @@ Tabulatr.prototype = {

updateTable: function(hash, forceReload) {
var $table = $('#'+ this.id);
var data;
if(hash.page !== undefined && !forceReload){
//old page should be stored
this.storePage = true;
// check if this page was already loaded
$table.find('tbody tr').hide();
if($table.find('tbody tr[data-page='+ hash.page +']').length > 0){
$table.find('tbody tr[data-page='+ hash.page +']').show();

this.updatePagination(hash.page,
$('.pagination[data-table='+ this.id +'] a:last').data('page'),
this.id);
if(this.isAPersistedTable){
data = this.createParameterString(hash, this.id);
localStorage[this.id] = JSON.stringify(data);
}
return;
}
}else{
this.storePage = false;
}
if(this.locked){ return; }
this.storePage = this.pageShouldBeStored(hash.page, forceReload);
if((this.storePage && this.retrievePage($table, hash)) || this.locked){ return; }
this.locked = true;
this.showLoadingSpinner();
this.loadDataFromServer(hash);
},

pageShouldBeStored: function(page, forceReload){
return page !== undefined && !forceReload;
},

retrievePage: function(table, hash){
table.find('tbody tr').hide();
if(table.find('tbody tr[data-page='+ hash.page +']').length > 0){
table.find('tbody tr[data-page='+ hash.page +']').show();

this.updatePagination(hash.page,
$('.pagination[data-table='+ this.id +'] a:last').data('page'),
this.id);
if(this.isAPersistedTable){
var data = this.createParameterString(hash, this.id);
localStorage[this.id] = JSON.stringify(data);
}
return true;
}
return false;
},

getDataForAjax: function(hash){
var data;
if(this.initialRequest && this.isAPersistedTable && localStorage[this.id]){
data = JSON.parse(localStorage[this.id]);
}else{
Expand All @@ -130,14 +137,18 @@ Tabulatr.prototype = {
localStorage[this.id] = JSON.stringify(data);
}
}
return data;
},

loadDataFromServer: function(hash){
$.ajax({
context: this,
type: 'GET',
url: $('table#'+ this.id).data('path'),
accepts: {
json: 'application/json'
},
data: data,
data: this.getDataForAjax(hash),
success: this.handleResponse,
complete: this.hideLoadingSpinner,
error: this.handleError
Expand Down

0 comments on commit 13c453d

Please sign in to comment.