var Post = Backbone.Model.extend();
var Posts = Backbone.Collection.extend({
model: Post,
url: 'https://jsonplaceholder.typicode.com/posts',
fetchSuccess: function(collection, response) {
console.log('Fetch response: ', response);
},
fetchError: function(collection, response) {
throw new Error('fetch error');
}
});
var PostView = Backbone.View.extend({
tagName: 'li',
render: function() {
this.$el.html(this.model.get('title'));
return this;
}
});
var PostsView = Backbone.View.extend({
render: function() {
var _this = this;
this.collection.each(function(post) {
// Put the current post in the child view
var _postView = new PostView({
model: post
});
// Render the post and append it to the DOM element of the postsView.
_this.$el.append(_postView.render().$el);
});
}
});
var posts = new Posts();
var postsView = new PostsView({
el: '#js-spa-container',
collection: posts
});
posts.fetch({
success: postsView.render.bind(postsView),
error: this.fetchError
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.