Last active
August 29, 2015 14:26
-
-
Save danielfdsilva/d2e5f2ad0f307a8091e6 to your computer and use it in GitHub Desktop.
DSO blog pagination
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
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