Created
June 4, 2012 16:36
-
-
Save zoghal/2869443 to your computer and use it in GitHub Desktop.
Ember.PaginationSupport
This file contains 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
App.collectionController = Em.ArrayProxy.create(Ember.PaginationSupport, { | |
content: [], | |
fullContent: App.store.findAll(App.Job), | |
totalBinding: 'fullContent.length', | |
didRequestRange: function(rangeStart, rangeStop) { | |
var content = this.get('fullContent').slice(rangeStart, rangeStop); | |
this.replace(0, this.get('length'), content); | |
} | |
}); |
This file contains 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
var get = Ember.get; | |
/** | |
@extends Ember.Mixin | |
Implements common pagination management properties for controllers. | |
*/ | |
Ember.PaginationSupport = Ember.Mixin.create({ | |
hasPaginationSupport: true, | |
total: 0, | |
rangeStart: 0, | |
rangeWindowSize: 10, | |
didRequestRange: Ember.K, | |
rangeStop: function() { | |
var rangeStop = get(this, 'rangeStart') + get(this, 'rangeWindowSize'), | |
total = get(this, 'total'); | |
if (rangeStop < total) { | |
return rangeStop; | |
} | |
return total; | |
}.property('total', 'rangeStart', 'rangeWindowSize').cacheable(), | |
hasPrevious: function() { | |
return get(this, 'rangeStart') > 0; | |
}.property('rangeStart').cacheable(), | |
hasNext: function() { | |
return get(this, 'rangeStop') < get(this, 'total'); | |
}.property('rangeStop', 'total').cacheable(), | |
nextPage: function() { | |
if (get(this, 'hasNext')) { | |
this.incrementProperty('rangeStart', get(this, 'rangeWindowSize')); | |
} | |
}, | |
previousPage: function() { | |
if (get(this, 'hasPrevious')) { | |
this.decrementProperty('rangeStart', get(this, 'rangeWindowSize')); | |
} | |
}, | |
page: function() { | |
return (get(this, 'rangeStart') / get(this, 'rangeWindowSize')) + 1; | |
}.property('rangeStart', 'rangeWindowSize').cacheable(), | |
totalPages: function() { | |
return Math.ceil(get(this, 'total') / get(this, 'rangeWindowSize')); | |
}.property('total', 'rangeWindowSize').cacheable(), | |
pageDidChange: function() { | |
this.didRequestRange(get(this, 'rangeStart'), get(this, 'rangeStop')); | |
}.observes('total', 'rangeStart', 'rangeStop') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment