Skip to content

Instantly share code, notes, and snippets.

@mattdelacdev
Created July 3, 2012 01:03
Show Gist options
  • Select an option

  • Save mattdelacdev/3036741 to your computer and use it in GitHub Desktop.

Select an option

Save mattdelacdev/3036741 to your computer and use it in GitHub Desktop.
Backbone: Cheatsheet
# 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