Now that we've covered all of View's capabilities, it's time to return to the question of how to render a View. Specifically, let's look at the main options available to you, which are explained in the sections that follow, when you overwrite the render
method.
The first, and perhaps the most obvious, approach for rendering is to use a simple, logic-less templating system. The render
method provided in the Backbone documentation is a perfect example of this, as it relies on the Underscore library's template
method:
render: function() { this.$el.html(this.template(this.model.toJSON())); return this; }
The template
method takes a string, which contains one or more specially designated sections, and then combines this string with an object, filling in the designated sections with that object's values. This is best explained with the following example:
var author ={ firstName: 'Isaac', lastName: 'Asimov', genre: 'science-fiction' }; var templateString...