Created
April 25, 2015 09:31
-
-
Save joostdevries/3020f584f7595a7f174f to your computer and use it in GitHub Desktop.
extend record array for pagination/infinite scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DS.AdapterPopulatedRecordArray.reopen({ | |
queryPageParam: 'page_number', | |
metaPageParam: 'currentPage', | |
metaTotalPagesParam: 'totalNumPages', | |
isPaginating: false, | |
nextPage: function (append) { | |
var meta = this.get('meta'); | |
if(meta[this.get('metaPageParam')]<meta[this.get('metaTotalPagesParam')]-1) | |
return this.getPage(meta[this.get('metaPageParam')] + 1, append); | |
}, | |
getPage: function (pageNumber, append) { | |
if(this.get('isPaginating')) { | |
return false; | |
} | |
var self = this; | |
var query = JSON.parse(JSON.stringify(this.get('query'))); // Ugly way of cloning | |
append = append===undefined?true:false; | |
query[this.get('queryPageParam')] = pageNumber; | |
this.store.findQuery(this.get('type'), query).then(function(nextRecordArray) { | |
self.loadPaginatedRecords(nextRecordArray, append); | |
}); | |
this.set('isPaginating', true); | |
}, | |
loadPaginatedRecords: function(nextRecordArray, append) { | |
if(!append) { | |
this.get('content').forEach(function(record) { | |
this.removeRecord(record); | |
}, this); | |
} | |
nextRecordArray.get('content').forEach(function(record) { | |
this.get('content').pushObject(record); | |
this.manager.recordArraysForRecord(record).add(this); | |
}, this); | |
this.set('meta', nextRecordArray.get('meta')); | |
this.set('isPaginating', false); | |
nextRecordArray.destroy(); | |
} | |
}); | |
DS.PromiseArray.reopen({ | |
nextPage: function() { | |
return this.get('content').nextPage.apply(this.get('content'), arguments); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment