How to improve templates in backbone js - Learning backbone js
This entry is part 7 of 14 in the series for A Complete Guide for Learning Backbone Js
- Introduction and Installation
- Representing your data in javascript
- Defining Models in backbone js
- Adding Validations in models backbone js
- Explaining views in backbone js
- How to use templates in backbone js
- How to improve templates in backbone js
- Collections in backbone js
- Collection views in backbone js
- Template helpers in backbone js
- How to use namespace in backbone js
- How to handle dom events in backbone js and define your custom events (Live Demo)
- Routing in backbone js
Agenda
So in the previous lesson, we learned how to create templates. It good but it can be improved further using external templates. So in the lesson, we will learn how to develop templates in backbone js.
What we have till now
From the previous episode, we have this PersonView class
So now we will take the template HTML code out into an external file and then call it here in out template. So let’s use index.html file and define a template there. So just paste this code in your index.html
file
We have defined this script as text/template
so that browser doesn’t execute this as standard javascript code. So once we have set the template, we can call this external template in our PersonView file using jquery. Here is the new PersonView
Class.
Output on Chrome Console
Pretty nice right. Now lets what we have on chrome developer tools.
Similarly what we had in the previous lesson. Beautiful and cool. So this makes our life much easier to define the templates in an external file and then just call it when we need it. And we can structure out HTML code of the template pretty easily.
That’s all for this lesson. Now thing about one object. How about displaying multiple objects. ul
and li
tags. The good thing is backbone js has collection view for that. Let’s see that in next lesson.
Source code
If you are facing any issues. Check out the source code files at github. I will be creating more and more directories in the same repo regarding each post. Still, if you have any doubts you can comment on the blog post itself, and I will try to reply back asap.