我对骨干很新,我遇到一个问题,即标题变量在这个简单的例子中未定义:
这是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
这是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>
我只是尝试遍历数组并将这些模型传递到视图中,并在页面上的aside元素中呈现这些视图。我知道我在这里遗漏了一些非常简单的东西,非常感谢任何帮助!
答案 0 :(得分:0)
我认为你的意思是:
_.each(models, function(model){
$('aside#facets').append(new FacetView({model: model}));
});