Created
January 21, 2013 20:08
-
-
Save xtrasmal/4588855 to your computer and use it in GitHub Desktop.
WP JSON-API Backbone.js-Posts
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
(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