Backbone Underscore Template
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js"></script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Cellar</title>
<%--Stylesheets --%>
<link href="<c:url value="/resources/css/styles.css" />" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><span class="title">Backbone Cellar</span></div>
<div id="sidebar"></div>
<div id="content">
<h2>Welcome to Backbone Cellar</h2>
<p>
This is a sample application part of of three-part tutorial showing how to build a CRUD application with Backbone.js.
</p>
</div>
<!-- Templates -->
<script type="text/template" id="tpl-wine-list-item">
<a href='#wines/{{ id }}'>{{ name }}</a>
</script>
<script type="text/template" id="tpl-wine-details">
<div class="form-left-col">
<label>Id:</label>
<input type="text" id="wineId" name="id" value="{{ id }}" disabled />
<label>Name:</label>
<input type="text" id="name" name="name" value="{{ name }}" required/>
<label>Grapes:</label>
<input type="text" id="grapes" name="grapes" value="{{ grapes }}"/>
<label>Country:</label>
<input type="text" id="country" name="country" value="{{ country }}"/>
<label>Region:</label>
<input type="text" id="region" name="region" value="{{ region }}"/>
<label>Year:</label>
<input type="text" id="year" name="year" value="{{ year }}"/>
</div>
<div class="form-right-col">
<img height="300" src="<c:url value='/resources/images/{{ picture }}' />" />
<label>Notes:</label>
<textarea id="description" name="description">{{ description }}</textarea>
</div>
</script>
JavaScript
// Using Mustache style markers
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
// Models
window.Wine = Backbone.Model.extend();
window.WineCollection = Backbone.Collection.extend({
model:Wine,
// url:"/mavenedge/wines"
});
//Views
window.WineListView = Backbone.View.extend({
tagName:'ul',
initialize:function () {
this.model.bind("reset", this.render, this);
},
render:function (eventName) {
_.each(this.model.models, function (wine) {
$(this.el).append(new WineListItemView({model:wine}).render().el);
}, this);
return this;
}
});
window.WineListItemView = Backbone.View.extend({
tagName:"li",
template:_.template($('#tpl-wine-list-item').html()),
render:function (eventName) {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
window.WineView = Backbone.View.extend({
template:_.template($('#tpl-wine-details').html()),
render:function (eventName) {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
// Router
var AppRouter = Backbone.Router.extend({
routes:{
"":"list",
"wines/:id":"wineDetails"
},
list:function () {
this.wineList = new WineCollection();
this.wineListView = new WineListView({model:this.wineList});
//this.wineList.fetch();
var rawData = [{ id: 1, name: 'test', grapes: 'red', country: 'usa', region: 'up', year: 2015, picture: 'blank', description: 'testing'}
,{ id: 2, name: 'test1', grapes: 'green', country: 'usa', region: 'up', year: 2014, picture: 'blank', description: 'testing'}
,{ id: 3, name: 'test2', grapes: 'yellow', country: 'usa', region: 'up', year: 2013, picture: 'blank', description: 'testing'}
,{ id: 4, name: 'test3', grapes: 'blue', country: 'usa', region: 'up', year: 2012, picture: 'blank', description:...