// Define the model
Playlist = Backbone.Model.extend();
// Define the collection
Playlists = Backbone.Collection.extend(
{
model: Playlist,
// Url to request when fetch() is called
url: 'http://gladtohelpyou.com/api/m/user/id/1/playlist/1',
parse: function(response) {
return response.results;
},
// Overwrite the sync method to pass over the Same Origin Policy
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'json',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
});
// Define the View
playlistView = Backbone.View.extend({
el: $('#wineList'),
events: {
"click .playlistClick" : "runPlaylist"
},
initialize: function() {
_.bindAll(this, 'render');
// create a collection
this.collection = new Playlists;
// Fetch the collection and call render() method
var that = this;
this.collection.fetch({
success: function () {
that.render();
}
});
},
// Use an extern template
template: _.template($('#wine-list-item').html()),
render: function() {
//_.bindAll(this, 'runPlaylist');
console.log(this.collection.toJSON());
// Fill the html with the template and the collection
$(this.el).html(this.template({ playlists: this.collection.toJSON() }));
},
runPlaylist: function(event) {
var name = $(event.target).data('name');
console.log(name);
}
});
var app = new playlistView();
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.