<script id="index-recipe" type="text/x-handlebars-template">
<h2>{{name}}</h2>
</script>
<script type="text/javascript">
var MyNamespace = { Views: {}};
$(document).ready(function () {
var template = Handlebars.compile($("#entry-template").html());
var templateRecipe = Handlebars.compile($("#index-recipe").html());
var recipes = [name = "Chicken Chilly", name = "Chicken Manchurian"];
MyNamespace.MyTagView = Backbone.View.extend({
template: templateRecipe,
initialize: function () {
this.render();
},
render: function () {
this.$el.html(this.template(this));
var ViewRecipes = new MyNamespace.Recipes({ collection: recipes });
this.$el.append(ViewRecipes.render().el);
return this;
}
});
MyNamespace.Recipes = Backbone.View.extend({
render: function () {
this.collection.forEach(function (recipe) {
var ViewRecipe = new Backbone.Recipe({ model: recipe });
this.$el.append(ViewRecipe.render().el);
}, this)
return this;
}
});
MyNamespace.Recipe = Backbone.View.extend({
render: function () {
this.$el.text("I am Recipe");
return this;
}
});
var View = new MyNamespace.MyTagView();
$("#content").html(View.el);
});
</script>
答案 0 :(得分:0)
编辑最后让它正常运作。 (见下面的链接。)
Recipe
应该是拥有“入门模板”的人。它应该使用该模板及其模型进行渲染。Backbone.Model
实例传递到Recipe
视图,将Backbone.Collection
实例传递到Recipes
视图。有一个拼写错误Backbone.Recipe
,当然不存在。
var ViewRecipe = new MyNamespace.Recipe({ model: recipe });
此外,在您发布的内容中,“#entry-template”没有模板定义。