Summary: demonstration of how to build Backbone. The more you write, the more you’ll have to maintain later. I know it might sound like overkill in the beginning, but as we go along we’ll add more and more complexity, including Models, Subviews and Collections. And include what remains book pdf frameworks in the index.
TODO write some awesome JS code! This will change the order in which scripts and the rest of HTML are loaded, and impact performance in large files. This is not a good idea for a real development or production code. We’ll come back to the home function later to add more logic for creating and rendering of a View. JSON object as a parameter to it. There are a multiple ways to proceed from now on, but the best practice is to use the el and template properties, which are magical, i. The template property has been assigned an Underscore.
Here is what the full code for our Backbone. Here is the full code of the index. We should add some data to play around with, and to hydrate our views. Note: This mock dataset can be easily substituted by assigning REST API endpoints of your back-end to url properties in Backbone.
This will allow users to go to index. SOMENAME and expect to see some information about an apple. It’s exactly the same name as the one that we’ve used in route. For now, we just output the string representation of the JSON object in the browser. This is not user-friendly at all, but later we’ll improve it by using a list and subviews.
In a template, we want to display figure, img and figcaption tags with specific values. Right now, the render function is responsible for both loading the data and rendering it. We expect to see an image with a caption. It’s a detailed view of an item, which in this case is an apple. Event Binding In real life, getting data does not happen instantaneously, so let’s refactor our code to simulate it. It’s a good thing that we have event binding in Backbone. Without it, we’ll have to pass a function that renders HTML as a callback to the data loading function, to make sure that the rendering function is not executed before we have the actual data to display.