Backbone views are used to reflect data; they are used to listen to events and respond accordingly. The idea is to organize the user interfaces into logical views that have methods to handle user interaction and can update the UI independently when the model data changes, without having to refresh the page.
To create a new view, use the backbone:view
subgenerator as follows:
$ yo backbone:view posts
The preceding command creates a new Backbone view located at app/scripts/views/posts.coffee
and a new Handlebars view template located at app/scripts/templates/posts.hbs
.
Backbone views use an optional render()
method that defines the logic to render a template. This example uses the Handlebars templating library to compile a collection with HTML that gets injected into the view's el
property.
Open the app/scripts/views/posts.coffee
file and add the following code:
define [ 'jquery', 'underscore', 'backbone', 'templates', 'collections/posts', ...