Created
December 19, 2012 15:08
-
-
Save ripter/4337343 to your computer and use it in GitHub Desktop.
Simple backbone example.
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
mycompany.Collections.MyCollection = Backbone.Collection.extend({ | |
model: mycompany.Models.MyModel | |
, url: '/rest/models' | |
}) |
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
mycompany.Models.MyModel = Backbone.Model.extend({ | |
url: '/rest/model' | |
}) |
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
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>'); | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment