0

我对骨干很陌生,并且在这个简单的示例中,标题变量出现为未定义的问题:

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

我只是试图遍历数组并将这些模型传递到视图中,然后在页面上的 side 元素中呈现这些视图。我知道我在这里遗漏了一些非常简单的东西,任何帮助将不胜感激!

4

1 回答 1

0

我想你的意思是:

_.each(models, function(model){
    $('aside#facets').append(new FacetView({model: model}));
});
于 2013-09-05T23:21:47.777 回答