// this example show one way to create a backbone collection
var ModelCollection = Backbone.Collection.extend(),
// this is the model class that will fill or model collection
Model = Backbone.Model.extend(),
//creating a template for our data
underscoreTemplate = _.template('<li><%= title %><br /><a href="<%= link %>"> <img src="<%= src %>" width="200"></a></li>'),
// this is our instance to backbone collection
myModelCollection = new ModelCollection (),
// static var that point to flickr api
JSONUrl = 'http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22&lang=en-us&format=json&jsoncallback=?';
//first have to load the flickr JSON (you can do that also using fetch() method)
jQuery.getJSON(JSONUrl,function(data){
// for each item..
$(data.items).each(function(i,item){
// ..we create a new model
var myModel = new Model ({
title: item.title,
src: item.media.m,
link: item.link
});
// and push it inside the collection
myModelCollection.add(myModel);
});
// for each model inside our modelCollection we print on page a <li>
_.each(myModelCollection.models, function(model,i){
// passing model data inside our underscore template
$('#result').append(underscoreTemplate(model.toJSON()));
})
});
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.