Skip to content

Instantly share code, notes, and snippets.

window.App = {
Models: {},
Collections: {},
Views: {}
}
window.App = {
Models: {},
Collections: {},
Views: {}
}
//= require backbone_rails_demo
//= require self
//= require app
App.Models.Album = Backbone.Models.extend()
App.album = new App.Models.Album({ title: 'Bitches Brew'});
// child {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
App.album.get('title');
// “Bitches Brew”
App.album.set({ title: 'Sketches of Spain' });
// child {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
App.album.get('title');
// ”A New Title”
# app/controllers/albums_controller.rb
def index
@albums = Album.where(user_id: params[:user_id])
respond_with(@albums)
end
App.Collections.Albums = Backbone.Collection.extend
model: App.Models.Album
url: "/users/2/albums"
App.albums = new App.Collections.Albums()
// child {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…}
App.albums.fetch();
// Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
App.albums.first()
// child {cid: "c1", attributes: Object, collection: child, _changing: false, _previousAttributes: Object…}
App.albums.first().get('title')
// “A Love Supreme"