DEV Community

Lam
Lam

Posted on

Backbone.Js Cheat Sheet

References

var Model = Backbone.Model.extend({
  // Single URL (string or function)
  url: '/account',
  url: function() { return '/account' },
Enter fullscreen mode Exit fullscreen mode
  // Both of these two work the same way
  url: function() { return '/books/' + this.id }),
  urlRoot: '/books'
})
Enter fullscreen mode Exit fullscreen mode
var obj = new Model({ url: ··· })
var obj = new Model({ urlRoot: ··· })
Enter fullscreen mode Exit fullscreen mode

[Models] Validation

var Model = Backbone.Model.extend({
  validate: function(attrs, options) {
    if (attrs.end < attrs.start) {
      return "Can't end before it starts"
    }
  }
})
Enter fullscreen mode Exit fullscreen mode

{: data-line="2"}

obj.validationError  //=> "Can't end before it starts"
obj.isValid()
obj.on('invalid', function (model, error) { ··· })
Enter fullscreen mode Exit fullscreen mode
// Triggered on:
obj.save()
obj.set({ ··· }, { validate: true })
Enter fullscreen mode Exit fullscreen mode

[Models] Methods

obj.id
obj.cid   // → 'c38' (client-side ID)
Enter fullscreen mode Exit fullscreen mode
obj.clone()
Enter fullscreen mode Exit fullscreen mode
obj.hasChanged('title')
obj.changedAttributes()  // false, or hash
obj.previousAttributes() // false, or hash
obj.previous('title')
Enter fullscreen mode Exit fullscreen mode
obj.isNew()
Enter fullscreen mode Exit fullscreen mode
obj.set({ title: 'A Study in Pink' })
obj.set({ title: 'A Study in Pink' }, { validate: true, silent: true })
obj.unset('title')
Enter fullscreen mode Exit fullscreen mode
obj.get('title')
obj.has('title')
obj.escape('title')     /* Like .get() but HTML-escaped */
Enter fullscreen mode Exit fullscreen mode
obj.clear()
obj.clear({ silent: true })
Enter fullscreen mode Exit fullscreen mode
obj.save()
obj.save({ attributes })
obj.save(null, {
  silent: true, patch: true, wait: true,
  success: callback, error: callback
})
Enter fullscreen mode Exit fullscreen mode
obj.destroy()
obj.destroy({
  wait: true,
  success: callback, error: callback
})
Enter fullscreen mode Exit fullscreen mode
obj.toJSON()
Enter fullscreen mode Exit fullscreen mode
obj.fetch()
obj.fetch({ success: callback, error: callback })
Enter fullscreen mode Exit fullscreen mode

[Models] Instantiating

var obj = new Model({ title: 'Lolita', author: 'Nabokov' })
Enter fullscreen mode Exit fullscreen mode
var obj = new Model({ collection: ··· })
Enter fullscreen mode Exit fullscreen mode

[Models] Defining

// All attributes are optional
var Model = Backbone.Model.extend({
  defaults: {
    'author': 'unknown'
  },
  idAttribute: '_id',
  parse: function() { ··· }
})
Enter fullscreen mode Exit fullscreen mode

[Views] Methods

view.$el.show()
view.$('input')
Enter fullscreen mode Exit fullscreen mode
view.remove()
Enter fullscreen mode Exit fullscreen mode
view.delegateEvents()
view.undelegateEvents()
Enter fullscreen mode Exit fullscreen mode

[Views] Instantiating

view = new View()
view = new View({ el: ··· })
Enter fullscreen mode Exit fullscreen mode

[Views] Defining

// All attributes are optional
var View = Backbone.View.extend({
  model: doc,
Enter fullscreen mode Exit fullscreen mode
  tagName: 'div',
  className: 'document-item',
  id: "document-" + doc.id,
  attributes: { href: '#' },
Enter fullscreen mode Exit fullscreen mode
  el: 'body',
Enter fullscreen mode Exit fullscreen mode
  events: {
    'click button.save': 'save',
    'click .cancel': function() { ··· },
    'click': 'onclick'
  },
Enter fullscreen mode Exit fullscreen mode
  constructor: function() { ··· },
  render: function() { ··· }
})
Enter fullscreen mode Exit fullscreen mode

List of events

  • Collection:

    • add (model, collection, options)
    • remove (model, collection, options)
    • reset (collection, options)
    • sort (collection, options)
  • Model:

    • change (model, options)
    • change:[attr] (model, value, options)
    • destroy (model, collection, options)
    • error (model, xhr, options)
  • Model and collection:

    • request (model, xhr, options)
    • sync (model, resp, options)
  • Router:

    • route:[name] (params)
    • route (router, route, params)

Reference

Top comments (0)