Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header class="container-fluid">
  <div class="page-header">
    <h1>Backbone.js Basics 4</h1>
    <p class="lead">Part 4 - Some Controller Logic and Model-View Communication.</p>
  </div>
</div>

<div class="container-fluid">
  <table class="table">
    <thead>
      <tr>
        <th>Manufacturer</th>
        <th>Model</th>
        <th>Stock</th>
        <th>Add/Remove</th>
      </tr>
    </thead>
    <tbody id="table-body"></tbody>
  </table>
</div>

<script type="text/template" id="surfboard-template">
  <td><%= manufacturer %></td>
  <td><%= model %></td>
  <td><%= stock %></td>
  <td>
    <button class="add-one">+1</button>
    <button class="minus-one">-1</button>
  </td>
</script>
              
            
!

CSS

              
                body{
  background-color: #F3F3F3;
}
              
            
!

JS

              
                // Surfboard model
var Surfboard = Backbone.Model.extend({
  
  defaults: {
    manufacturer: '',
    model: '',
    stock: 0
  },
  
  addOne: function() {
    this.set({
      stock: this.get('stock') + 1
    });
    // probably update a database
  },
  
  minusOne: function() {
    this.set({
      stock: this.get('stock') - 1
    });
    // probably update a database
  }
  
});

// New instance of Surfboard model - board1
var board1 = new Surfboard({
  manufacturer: 'Channel Islands',
  model: 'Whip',
  stock: 12
});

// New instance of Surfboard model - board2
var board2 = new Surfboard({
  manufacturer: 'Lost',
  model: 'Sub Scorcher',
  stock: 9
});

// New instance of Surfboard model - board3
var board3 = new Surfboard({
  manufacturer: 'Firewire',
  model: 'Spitfire',
  stock: 5
});

// Create Surfboards Collection
var SurfboardsCollection = Backbone.Collection.extend({
  model: Surfboard
});

// Create new instance of SurfboardsCollection
// and add three model instances to it.
var Surfboards = new SurfboardsCollection;
Surfboards.add(board1);
Surfboards.add(board2);
Surfboards.add(board3);

// Create Surfboards View
var SurfboardsView = Backbone.View.extend({

  el: '#table-body',
  
  initialize: function() {
    this.render();
  },
  
  render: function() {
    this.$el.html('');

    Surfboards.each(function(model) {
      var surfboard = new SurfboardView({
        model: model
      });

      this.$el.append(surfboard.render().el);
    }.bind(this));

    return this;
  }
  
});

// Create Surfboard View
var SurfboardView = Backbone.View.extend({

  tagName: 'tr',
  
  events: {
    'click .add-one': 'addOne',
    'click .minus-one': 'minusOne'
  },
  
  template: _.template($('#surfboard-template').html()),
  
  initialize: function() {
    this.listenTo(this.model, "change", this.render);
  },
  
  render: function() {
    this.$el.html(this.template(this.model.attributes));
    return this;
  },
  
  addOne: function(e) {
    e.preventDefault();
    this.model.addOne();
  },
  
  minusOne: function(e) {
    e.preventDefault();
    this.model.minusOne();
  }
  
});

// Launch app
var app = new SurfboardsView;
              
            
!
999px

Console