Skip to content

Instantly share code, notes, and snippets.

@danielfdsilva
Last active August 29, 2015 14:26
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 danielfdsilva/d2e5f2ad0f307a8091e6 to your computer and use it in GitHub Desktop.
Save danielfdsilva/d2e5f2ad0f307a8091e6 to your computer and use it in GitHub Desktop.
DSO blog pagination
DSO.Views = DSO.Views || {};
(function() {
DSO.Views.BlogList = function(opts) {
this.container = opts.container;
var _posts = [];
var _self = this;
var _template = JST['card-list.ejs'];
var _perPage = 9;
var _curPage = 1;
this.init = function() {
this.requestAndRender();
};
this.requestAndRender = function() {
_self.render(true);
_self.request(function() {
_self.render();
});
};
this.request = function(callback) {
$.get('/api/blog_posts.json').success(function(data) {
_posts = data;
callback(data);
});
};
this.render = function(loading) {
loading = loading || false;
var posts = _posts.slice(0, _curPage * _perPage);
this.container.html(_template({
posts: posts,
loading: loading,
type: 'blog',
more: _posts.length > posts.length,
count: {
total: _posts.length,
showing: posts.length,
}
}));
this.addEventListeners();
};
this.addEventListeners = function() {
this.container.find('[data-load-more]').click(function(e) {
e.preventDefault();
_curPage++;
_self.render();
});
};
// GO!
this.init();
};
})();
// Initialization code.
// This is placed in the page where the list should be.
$(document).ready(function() {
new DSO.Views.BlogList({container: $('[data-view-blog-list]')});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment