Skip to content

Instantly share code, notes, and snippets.

@fpauser
Created February 3, 2015 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpauser/d0d7b72e9a9d7fead1db to your computer and use it in GitHub Desktop.
Save fpauser/d0d7b72e9a9d7fead1db to your computer and use it in GitHub Desktop.
import Ember from 'ember';
export default Ember.Object.extend({
store: null,
model: null,
currPage: 1,
pageSize: 30,
cacheSize: 5,
params: null,
request: null,
cache: null,
meta: null,
pageRecords: null,
countFiltered: Ember.computed.alias('meta.count_filtered'),
countTotal: Ember.computed.alias('meta.count_total'),
initialize: function() {
Ember.assert('Erorr: Pager must have a store!', !Ember.isNone(this.get('store')));
Ember.assert('Erorr: Pager must have a model!', !Ember.isNone(this.get('model')));
this.set('pageRecords', []);
}.on('init'),
isLoading: Ember.computed.equal('request.isPending', true),
metaIsEmpty: Ember.computed.empty('meta'),
isRefreshing: Ember.computed.and('isLoading', 'metaIsEmpty'),
prevButtonIsDisabled: Ember.computed.or('isLoading', 'isFirstPage'),
nextButtonIsDisabled: Ember.computed.or('isLoading', 'isLastPage'),
load: function(params) {
this.setProperties({
params: params,
request: null,
cache: [],
meta: null,
currPage: 1
});
this.updatePageRecords();
},
updatePageRecords: function() {
var currPage = this.get('currPage');
var pageSize = this.get('pageSize');
var cacheSize = this.get('cacheSize');
var cache = this.get('cache');
var start = (currPage - 1) * pageSize;
var pageRecords = this.get('pageRecords');
var cacheRecords = cache.slice(start, start + pageSize);
pageRecords.setObjects(cacheRecords);
if (Ember.isEmpty(pageRecords)) {
var self = this;
var store = this.get('store');
var model = this.get('model');
var params = this.get('params');
var data = {
filter: params.filter,
orderColumn: params.orderColumn,
orderDirection: params.orderDirection,
offset: start,
limit: pageSize * cacheSize
};
var request = store.find(model, data);
this.set('request', request);
request.then(function(data) {
if (request === self.get('request')) {
var meta = data.get('meta');
self.set('meta', meta);
var records = data.get('content');
cache.pushObjects(records);
var cacheRecords = cache.slice(start, start + pageSize);
pageRecords.setObjects(cacheRecords);
}
});
}
}.observes('currPage'),
maxPage: function() {
var countFiltered = this.get('countFiltered');
var pageSize = this.get('pageSize');
var page = Math.ceil(countFiltered / pageSize);
return (page === 0) ? 1 : page;
}.property('meta.count_filtered', 'pageSize'),
currPageStart: function() {
var currPage = this.get('currPage');
var pageSize = this.get('pageSize');
var index = (currPage - 1) * pageSize + 1;
return index;
}.property('currPage', 'pageSize', 'countFiltered'),
currPageStop: function() {
var maxPage = this.get('maxPage');
var currPage = this.get('currPage');
var pageSize = this.get('pageSize');
var countFiltered = this.get('countFiltered');
var index = null;
if (currPage < maxPage) {
index = currPage * pageSize;
} else {
index = countFiltered;
}
return index;
}.property('currPage', 'pageSize', 'countFiltered'),
gotoPrevPage: function() {
if (!this.get('isFirstPage')) {
this.decrementProperty('currPage');
}
},
gotoNextPage: function() {
if (!this.get('isLastPage')) {
this.incrementProperty('currPage');
}
},
isFirstPage: function() {
return this.get('currPage') === 1;
}.property('currPage'),
isLastPage: function() {
var currPage = this.get('currPage');
var maxPage = this.get('maxPage');
return currPage === maxPage;
}.property('currPage', 'maxPage')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment