O'Reilly logo

CoffeeScript, 2nd Edition by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Presenting Data with Views

A Backbone view is a do-it-yourself kit for turning a model into HTML. Unlike other JavaScript frameworks, Backbone doesn’t perform rendering or respond to input events or interact with its model in any way unless you explicitly tell it to. So let’s roll up our sleeves and write some display logic.

We’ll start with a view class for our Card model. All it needs to do is render the model into markup and save changes in the description and due date fields to the model:

 class​ window.CardView ​extends​ Backbone.View
  render: ->
  html = JST[​'templates/card'​]
  description: @model.get(​'description'​)
  dueDate: @model.get(​'due-date'​)
 
  @$el.html html
 @
 
  events:
 'change [name=card-description]' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required