var DocumentModel = Backbone.Model.extend({});
var document = new DocumentModel();
document.on("change:title", function (doc, newTitle) {
console.log("Title changed to", newTitle);
});
document.set("title", "Hello World");
var DocumentCollection = Backbone.Collection.extend({
model: DocumentModel,
url: "api/documents"
});
var documents = new DocumentCollection();
documents.on("add", function (doc) {
console.log("Got document from server", doc);
});
documents.fetch();
var DocumentView = Backbone.View.extend({
className: "document",
events: {
"click .save": "saveDocument"
},
render: function () {
// "The default implementation of render is a no-op."
},
saveDocument: function () {
// ...
}
});
var view = new DocumentView({
model: document
});
// Somehow render and attach to the right place in the DOM.
var DocumentView = Marionette.Layout.extend({
template: Handlebars.compile(template),
regions: {
imageBrowser: "#image-browser"
},
onRender: function () {
this.imageBrowser.show(new ImageBrowserView({}));
},
//...
});
region.show(new DocumentView({
model: document
}));
var TableView = Marionette.CollectionView.extend({
itemView: DocumentThumbnailView
});
var view = new TableView({
collection: documents
});
var DocumentView = Marionette.Layout.extend({
bindings: {
".title": "title",
".content": "content",
".alert.short-title": {
observe: "title",
visible: function (title) {
return title.length < 5;
}
}
},
// ...
});
var DocumentModel = Associations.AssociatedModel.extend({
relations: [{
type: Associations.One,
key: "author",
relatedModel: AuthorModel
}, {
type: Associations.Many,
key: "comments",
collectionType: CommentCollection
}]
});
document.get("author").set("name", "John Smith");
document.set("author.name", "John Smith");