Created
July 3, 2012 01:03
-
-
Save mattdelacdev/3036741 to your computer and use it in GitHub Desktop.
Backbone: Cheatsheet
This file contains hidden or 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
| # Backbone | |
| - Models - Store data and trigger cahnge events | |
| `window.Album = Backbone.Model.extend({});` | |
| album = new Album({title: 'Abbey Road', artist: 'The Beatles'}) | |
| -- Get an attribute | |
| album.get('title') | |
| -- Set an attribute | |
| album.set({key: value}) | |
| -- Model persistent | |
| album.isNew() | |
| -- serialize attributes | |
| album.toJSON() | |
| - Views - render model data - observe and re-render | |
| View are like controllers in Rails | |
| window.AlbumVIew = Backbone.View.extend({ | |
| tagName: 'li' | |
| className: 'ablum', | |
| initializa: function() { | |
| _.binAll(this, 'render'); | |
| thi s.mode.bind('change', this.render); | |
| this.template = _.template($('#album-template').html()); | |
| }, | |
| render: function() { | |
| $(this.el).html(this.template(this.model.toJSON()); | |
| return this; | |
| } | |
| }); | |
| albumView = new AlbumView({model: album}) | |
| - Collections - manage group of models | |
| Need an implementation for every model | |
| window.Albums = Backbone.Collection.extend({ | |
| model: Album, | |
| url: '/albums' | |
| }); | |
| albums = new Albums | |
| albums.fetch | |
| albums.map(function(album) { | |
| album.get('title') | |
| }); | |
| # Router | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment