异常>:TypeError undefined不是函数

时间:2013-12-18 00:21:55

标签: backbone.js

<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>

1 个答案:

答案 0 :(得分:0)

编辑最后让它正常运作。 (见下面的链接。)

  1. Recipe应该是拥有“入门模板”的人。它应该使用该模板及其模型进行渲染。
  2. 将实际Backbone.Model实例传递到Recipe视图,将Backbone.Collection实例传递到Recipes视图。

  3. 有一个拼写错误Backbone.Recipe,当然不存在。

    var ViewRecipe = new MyNamespace.Recipe({ model: recipe });
    

    此外,在您发布的内容中,“#entry-template”没有模板定义。

    Here's a working example.