When a user first comes to our website, we don't want to display the game view right away. Most web applications will have some kind of home view, or welcoming view, explaining the purpose of the application. We could make that a server-side template, but we're going to make it a Backbone view instead. Here's the code of the HomeView
class:
var HomeView = Backbone.View.extend({ template: $('#levels').html(), events: { 'click a' : 'chooseLevel' }, render: function () { this.el.innerHTML = this.template; return this; }, chooseLevel: function (evt) { evt.preventDefault(); this.remove(); Backbone.history.navigate(evt.currentTarget.pathname, { trigger: true }); return false; } });
We get the template with the id
property of home
from the index.ejs
file. The render
method is simple. The events
object listens for clicks on anchor elements and calls the chooseLevel
method. We've seen a method like this before; it just prevents...