5 category: JavaScript libraries
11 .on('event', callback)
12 .on('event', callback, context)
27 .once('event', callback) // Only happens once
33 object.off('change', onChange) // just the `onChange` callback
34 object.off('change') // all 'change' callbacks
35 object.off(null, onChange) // `onChange` callback for all events
36 object.off(null, null, context) // all callbacks for `context` all events
43 object.trigger('event')
47 view.listenTo(object, event, callback)
54 * `add` (model, collection, options)
55 * `remove` (model, collection, options)
56 * `reset` (collection, options)
57 * `sort` (collection, options)
60 * `change` (model, options)
61 * `change:[attr]` (model, value, options)
62 * `destroy` (model, collection, options)
63 * `error` (model, xhr, options)
65 * Model and collection:
66 * `request` (model, xhr, options)
67 * `sync` (model, resp, options)
70 * `route:[name]` (params)
71 * `route` (router, route, params)
78 // All attributes are optional
79 var View = Backbone.View.extend({
85 className: 'document-item',
86 id: "document-" + doc.id,
87 attributes: { href: '#' },
96 'click button.save': 'save',
97 'click .cancel': function() { ··· },
103 constructor: function() { ··· },
104 render: function() { ··· }
111 view = new View({ el: ··· })
126 view.delegateEvents()
127 view.undelegateEvents()
135 // All attributes are optional
136 var Model = Backbone.Model.extend({
141 parse: function() { ··· }
148 var obj = new Model({ title: 'Lolita', author: 'Nabokov' })
152 var obj = new Model({ collection: ··· })
159 obj.cid // → 'c38' (client-side ID)
167 obj.hasChanged('title')
168 obj.changedAttributes() // false, or hash
169 obj.previousAttributes() // false, or hash
170 obj.previous('title')
178 obj.set({ title: 'A Study in Pink' })
179 obj.set({ title: 'A Study in Pink' }, { validate: true, silent: true })
186 obj.escape('title') /* Like .get() but HTML-escaped */
191 obj.clear({ silent: true })
196 obj.save({ attributes })
198 silent: true, patch: true, wait: true,
199 success: callback, error: callback
207 success: callback, error: callback
217 obj.fetch({ success: callback, error: callback })
223 var Model = Backbone.Model.extend({
224 validate: function(attrs, options) {
225 if (attrs.end < attrs.start) {
226 return "Can't end before it starts"
234 obj.validationError //=> "Can't end before it starts"
236 obj.on('invalid', function (model, error) { ··· })
242 obj.set({ ··· }, { validate: true })
248 var Model = Backbone.Model.extend({
249 // Single URL (string or function)
251 url: function() { return '/account' },
255 // Both of these two work the same way
256 url: function() { return '/books/' + this.id }),
262 var obj = new Model({ url: ··· })
263 var obj = new Model({ urlRoot: ··· })
269 - [Backbone website](http://backbonejs.org/) _(backbonejs.org)_
270 - [Backbone patterns](http://ricostacruz.com/backbone-patterns/) _(ricostacruz.com)_