public
Created

Simple backbone example.

  • Download Gist
myCollection.js
JavaScript
1 2 3 4
mycompany.Collections.MyCollection = Backbone.Collection.extend({
model: mycompany.Models.MyModel
, url: '/rest/models'
})
myModel.js
JavaScript
1 2 3
mycompany.Models.MyModel = Backbone.Model.extend({
url: '/rest/model'
})
myView.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
mycompany.Views.MyView = Backbone.View.extend({
initialize: function() {
// good practice but not nessary for this simple example
_.bindAll(this);
// create the collection
this.collection = new mycompany.Collections.MyCollection();
// when we get the data back call render
this.collection.on('reset', this.render);
// fetch the items from the server
this.collection.fetch();
}
, render: function() {
var self = this;
// loop over the models and render each one
self.collection.each(function(model){
// do your rendering
self.$el.append('<div>' + model.get('someProperty') + '</div>');
})
}
})

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.