Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created January 21, 2013 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtrasmal/4588855 to your computer and use it in GitHub Desktop.
Save xtrasmal/4588855 to your computer and use it in GitHub Desktop.
WP JSON-API Backbone.js-Posts
(function($) {
// MODEL
var Post = Backbone.Model.extend();
// COLLECTION
var PostList = Backbone.Collection.extend({
model: Post,
url: 'http://localhost/Wordpress/Twentytwelve/api/get_recent_posts/',
parse: function(resp) {
return resp.posts;
}
});
// VIEW
var PostsView = Backbone.View.extend({
template: _.template($('#postlist_template').html()),
render: function(eventName) {
_.each(this.model.models, function(post) {
var lPostName = post.attributes['title'];
var lTemplate = this.template(post.toJSON());
$(this.el).append(lTemplate);
}, this);
return this;
}
});
// INSTANCE
var lPosts = new PostList;
// // Poller
// var poller = Backbone.Poller.get(lPosts, {
// delay: 2000
// });
// poller.on('success', function(lPosts) {
// console.info('another successful fetch!');
// App.render();
// });
// poller.on('complete', function(lPosts) {
// console.info('hurray! we are done!');
// });
// poller.on('error', function(lPosts) {
// console.error('oops! something went wrong');
// });
// poller.start();
// APP VIEW
var AppView = Backbone.View.extend({
el: "body",
render: function() {
var lPostsView = new PostsView({
model: lPosts
});
var lHtml = lPostsView.render().el;
$('#posts').html(lHtml);
},
initialize: function() {
var lOptions = {};
lOptions.success = this.render;
lPosts.fetch(lOptions);
}
});
var App = new AppView;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment