Created
August 31, 2015 18:55
-
-
Save code-for-coffee/41e0797087a93a57469c to your computer and use it in GitHub Desktop.
Render a Collection bound from an API with Backbone.js
This file contains 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
var app = app || {}; | |
var active = active || {}; | |
//blueprints | |
app.model = Backbone.Model.extend(); | |
app.collection = Backbone.Collection.extend({ | |
model: app.model, | |
url: '/api/people' // path to your API | |
}); | |
app.modelView = Backbone.View.extend({ | |
initialize: function() { | |
// every modelView should have a model | |
//this.model | |
this.template = _.template($('#people-template').html()); // point to an underscore template of your choice | |
this.render(); | |
}, | |
render: function() { | |
var data = this.model.attributes; | |
this.$el.append(this.template(data)); | |
} | |
}); | |
app.collectionView = Backbone.View.extend({ | |
initialize: function() { | |
var that = this; | |
// every collectionView should have a collection | |
this.collection.on('sync', function() { | |
that.render(); | |
}); | |
// retrieve data from my API 'all get' route | |
this.collection.fetch(); | |
this.$el.html(''); // empty out any content inside of my $el | |
}, | |
render: function() { | |
var collection = this.collection.models; | |
for (var model in collection) { | |
//console.log(collection[model].attributes); | |
// memory purposes | |
new app.modelView({ | |
el: $('#people-list'), // change this to whatever DOM element youn want to use | |
model: collection[model] | |
}); | |
} | |
} | |
}); | |
//end blueprints | |
$(document).ready(function(event) { | |
// instantiate collection + collectionView | |
active.collection = new app.collection(); | |
active.collectionView = new app.collectionView({ | |
collection: active.collection, | |
el: $('#people-list') // change this to whatever DOM element youn want to use | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment