Thursday, 5 September 2013

Backbone: Uncaught ReferenceError: title is not defined

Backbone: Uncaught ReferenceError: title is not defined

I am very new to backbone, and I am having an issue where the title
variable is coming up as undefined in this simple example:
Here is the JS:
(function() {
var Facet = Backbone.Model.extend({
defaults: {
title : ''
}
});
var models = [
new Facet({title: "Woah"}),
new Facet({title: "Yeah"})
];
var FacetView = Backbone.View.extend({
el: 'div',
template: _.template($("#facet_template").html()) ,
initialize: function(){
this.render();
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
_(models).each(function(model){
$('aside#facets').append(new FacetView({model: model}));
});
})();
// () means it is immediately invoked
Here is the HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/Underscore.js"></script>
<script type="text/javascript" src="js/Backbone.js"></script>
<link href="css/site.css" rel="stylesheet" />
</head>
<body id="page">
<h1>Example Search Page</h1>
<aside id="facets" class="three columns">
</aside>
<div id="searchResults">
</div>
</body>
<script type="text/template" id="facet_template">
<label>
<%= title %>
<input type="checkbox" class="facet" />
</label>
</script>
<script type="text/javascript" src="js/TourFinder.js" defer="true"></script>
</html>
I am simply trying to loop through the array and pass those models into
the view and render those views in the aside element on the page. I know I
am missing something extremely simple here and any help would be greatly
appreciated!

No comments:

Post a Comment