How to use backbone

Comprehensive backbone code examples:

How to use backbone.ChildViewContainer:

38
39
40
41
42
43
44
45
46
// we need to own initialize, this includes marionette.View()
// AND backbone.View()
constructor: function(options){
    options || (options = {});

    this.children = new backbone.ChildViewContainer();

    /* >>> BEGIN marionette.View() override */
     _.bindAll(this, 'render', 'invalidateView');

How to use backbone.Attributes:

207
208
209
210
211
212
213
214
215
216

});
module.exports = Photos;

},{"backbone":10,"moment":11,"templates/photos.jst":5}],9:[function(require,module,exports){
// Backbone.Attributes.js 0.7.1
// ---------------

//     (c) 2014 Adam Krebs
//     Backbone.Attributes may be freely distributed under the MIT license.

How to use backbone.Events:

8
9
10
11
12
13
14
15
16
module.exports = class Api {
  constructor(options) {
    if (!options.state) throw 'Must pass options.state, a State'
    if (!options.globalActions) throw 'Must pass options.globalActions, an Object of functions'

    // Inherit from Backbone.Events
    for (const f in Backbone.Events) {
      this[f] = Backbone.Events[f]
    }

How to use backbone.js:

27
28
29
30
31
32
33
34
35
36
0. [Architecting JS Apps at Eventbrite](#architecting-js-apps-at-eventbrite)
0. [Debugging common issues](#debugging-common-issues)
0. [Testable Modular JS with Backbone, Jasmine & Sinon](#testable-modular-js-with-backbone-jasmine--sinon)


## Backbone.js

From the [Backbone.js](http://backbonejs.org/) docs:

> Backbone.js gives structure to web applications by providing **models** with key-value binding and custom events, **collections** with a rich API of enumerable functions, **views** with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

How to use backbone.sync:

33
34
35
36
37
38
39
40
41
42

save: function (options) {
  this.each(function (layerDefModel, index) {
    layerDefModel.set('order', index, { silent: true });
  });
  Backbone.sync('update', this, options);
},

toJSON: function () {
  return {

How to use backbone.Form:

12
13
14
15
16
17
18
19
20
21
var INPUT_TYPE_MAP = {
  'size': InputNumber,
  'color': InputColor
};

Backbone.Form.editors.Fill = Backbone.Form.editors.Base.extend({
  className: 'Form-InputFill CDB-OptionInput CDB-Text js-input',

  events: {
    focus: function () {

How to use backbone.Router:

55
56
57
58
59
60
61
62
63
64
);
var userModel = new UserModel(userData, { configModel: configModel });
var modals = new ModalsServiceModel();
var tableModel = new TableModel(tableData, { parse: true, configModel: configModel });
var editorModel = new EditorModel();
var Router = new Backbone.Router();
var visModel = new VisTableModel(visData, { configModel: configModel });
var layersCollection = new Backbone.Collection(layersData);
var layerDataModel = layersCollection.find(function (mdl) {
  var kind = mdl.get('kind');

How to use backbone.history:

65
66
67
68
69
70
71
72
73

  // Initialize router
  window.router = new Router({ user: model });

  // Start responding to routes
  Backbone.history.start();
},
error: function(model, res, options) {
  var apiStatus = status.githubApi(function(res) {

How to use backbone.Marionette:

119
120
121
122
123
124
125
126
127
return Marionette.ItemView.extend({ /* do something here */ });

// bad (access Marionette from Backbone)
var Backbone = require('backbone');

return Backbone.Marionette.ItemView.extend({ /* do something here */ });
```

**[⬆ back to top](#table-of-contents)**

How to use backbone.ajax:

47
48
49
50
51
52
53
54
55
56
 * Rebuild this project.
 * @returns {object} jqXHR object
 **/
build: function() {
  this.set('status', 'building');
  return Backbone.ajax({
    dataType: 'json',
    type: 'GET',
    url: this.url() + '/build'
  });

How to use backbone.Collection:

30
31
32
33
34
35
36
37
38
39
this._layersFactory = options.layersFactory;

attrs = attrs || {};

this.layers = options.layersCollection || new Layers();
this.geometries = new Backbone.Collection();

var center = attrs.center || this.defaults.center;
if (typeof center === 'string') {
  center = JSON.parse(center);

How to use backbone.Model:

4
5
6
7
8
9
10
11
12
13

var MediaController = wp.media.controller.State.extend({

        initialize: function(){

                this.props = new Backbone.Model({
                        currentShortcode: null,
                        action: 'select',
                        search: null,
                        insertCallback: this.insertCallback,

How to use backbone.View:

2
3
4
5
6
7
8
9
10
11
var utils = require('../utils')
var lang = require('../translations')
var autosize = require('autosize')
var $ = require('jquery')

var EditorView = Backbone.View.extend({
  el: '#editor',

  initialize: function (options) {
    _.bindAll(