0

我有一个填充了默认数据的主干模型和一个模板,我认为应该在页面加载时显示该数据,但我无法显示它。我是 Backbone 的新手,所以任何指针和解释都将不胜感激。下面的代码:

(function($) {
window.Result = Backbone.Model.extend ({
    defaults: {
    name: 'player name',
    position: '1',
    stroke: '12',
    nationality: 'ENG',
    r1: '11',
    r2: '22',
    r3: '33',
    r4: '44',
    par: '-9',
    holeAgg: '399',
    winnings: '400,000'
    }
});

window.ResultsView = Backbone.View.extend({
    initialize: function() {
        tagName: 'td',
        this.template = _.template($('#results').html(), {} ); 
    },

    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent); 
        return this;
    }
});

window.Results = Backbone.Collection.extend({

});  

})(jQuery);

和 html

    <head>
    <title>Backbone</title>

    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
    <script src="./assets/js/vendor/underscore.js"></script>
    <script src="./assets/js/vendor/backbone.js"></script>

    <script src="./assets/js/application.js"></script>

    <link href="./assets/css/bootstrap.css" rel="stylesheet">
    <link href="./assets/css/custom.css" rel="stylesheet">

  </head>
  <body>
    <div id="main">

      <script type="text/template" id="results">
        <table class="results table">
          <th>Pos</th>
          <th>Player</th>
          <th>R1</th>
          <th>R2</th>
          <th>R3</th>
          <th>R4</th>
          <th>Score</th>
          <th>ToPar</th>
          <th>Earnings(US$)</th>
          <tr>
            <td class="pos"><%= position %></td>
            <td class="name"><%= name %></td>
            <td class="score_R1"><%= r1 %></td>
            <td class="score_R2"><%= r2 %></td>
            <td class="score_R3"><%= r3 %></td>
            <td class="score_R4"><%= r4 %></td>
            <td class="score"><%= holeAgg %></td>
            <td class="vspar"><%= par %></td>
            <td class="winnings"><%= winnings %></td>
          </tr>
        </table>
    </script>

    </div>


    </body>
    </html>
4

1 回答 1

1

你初始化你的视图吗?没有代码..我认为你的视图应该是这样的

window.ResultsView = Backbone.View.extend({
    // with no tagName: 'td'
    template: _.template($('#results').html(), {} ); 

    initialize: function() {
    },

    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent); 
        return this;
    }
});

然后初始化它

model = new Result();
var view = new ResultsView({model: model});
view.render();

你总是可以在这里找到任何帮助http://addyosmani.github.io/backbone-fundamentals/

于 2013-08-21T22:37:49.337 回答