Skip to content

Instantly share code, notes, and snippets.

@hobberwickey
Created February 18, 2015 01:50
Show Gist options
  • Save hobberwickey/8f4af952e91ac5065739 to your computer and use it in GitHub Desktop.
Save hobberwickey/8f4af952e91ac5065739 to your computer and use it in GitHub Desktop.
Example of a Pagination mixin for smartMixins
<script>
var Pagination = {
change_page: function(){
this.paged_data = this.data.slice((this.page - 1) * this.per_page, this.page * this.per_page);
},
next_page: function(){
if (this.page < this.total_pages){
this.page++;
}
},
previous_page: function(){
if (this.page > 1){
this.page--;
}
},
_created: function(){
if (!this.data) this.data = [];
if (!this.paged_data) this.paged_data = [];
if (!this.total_pages) this.total_pages = 0;
if (!this.page) this.page = 1;
if (!this.per_page) this.per_page = 10;
},
domReady_: function(){
this.total_pages = Math.ceil(this.data.length / this.per_page);
this.paged_data = this.data.slice((this.page - 1) * this.per_page, this.page * this.per_page);
new PathObserver(this, 'page').open(this.change_page.bind(this));
new PathObserver(this, 'data').open(function(){
this.total_pages = Math.ceil(this.data.length / this.per_page);
this.change_page();
}.bind(this));
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment