1

我对 Backbone 和下划线中的第一个模板化视图不太满意。

我从 Underscore 库中收到“未定义玩家错误”。

这是我的模型:

define([
    'domLib',
    'underscore',
    'backbone',
    'router'
    ],
    function($, _, Backbone, Router) {

        var PlayerModel = Backbone.Model.extend({
            defaults: {
                username: '',
                rank: 0,
                score: 0
            }
        });

        return PlayerModel;

});

这是我的收藏:

define([
    'domLib',
    'underscore',
    'backbone',
    'router',
    'model/player'
    ],
    function($, _, Backbone, Router, PlayerModel) {

        var LeaderboardCollection = Backbone.Collection.extend({
            model: PlayerModel,
            url: '/hyc-web/leaderBoard/topOverall?count=5'
        });

        return LeaderboardCollection;
});

相关视图:

define([
    'domLib',
    'underscore',
    'backbone',
    'router',
    'collection/leaderboard',
    'text!template/leaderboard.html'
    ],
    function($, _, Backbone, Router, LeaderboardCollection, LeaderboardTemplate) {

        var LeaderboardView = Backbone.View.extend({
            el: '#leaderboard',
            template: _.template(LeaderboardTemplate),
            initialize: function(){
                this.collection = new LeaderboardCollection();              
                this.collection.on('reset', this.render, this); 
                this.collection.fetch();                                        
            },
            render: function(){         
                console.log(this.collection.models);
                this.$el.html(this.template, {players: this.collection.models});
            }
        });

        return LeaderboardView;
});

和模板本身:

<table class="table table-bordered">
    <thead>                             
        <tr>
            <td>Rank</td>
            <td>Player</td>
            <td>Score</td>
        </tr>
    </thead>
    <tfoot>
        <!-- Logged in user details here -->
    </tfoot>
    <tbody>
        <% _.each(players, function(player){ %>
            <tr>
                <td><%= player.rank %></td>
                <td><%= player.username %></td>
                <td><%= player.highScore %></td>
            </tr>           
        <% }); %>                                   
    </tbody>
</table>

一切都很顺利,直到我尝试连接到实际的数据服务层。我不明白为什么这不是模板化 JSON 数组?

4

2 回答 2

3

您可能希望您的渲染方法看起来更像:

render: function() {
  this.$el.html(this.template({ players: this.collection.toJSON() });
}

您需要通过视图的 template() 函数而不是 jQuery html() 函数传递数据上下文。此外,您希望使用 collection.toJSON() 将集合转换为 JSON,而不是使用 collection.models 传入模型的原始数组。

于 2013-04-14T14:20:35.047 回答
0

在文档中进行了一些挖掘之后,我想出了这个:

define([
    'domLib',
    'underscore',
    'backbone',
    'router',
    'collection/leaderboard',
    'text!template/leaderboard.html'
    ],
    function($, _, Backbone, Router, LeaderboardCollection, LeaderboardTemplate) {

        var LeaderboardView = Backbone.View.extend({
            el: '#leaderboard',
            template: _.template(LeaderboardTemplate),
            initialize: function(){
                var self = this;
                self.collection = new LeaderboardCollection();
                self.collection.fetch({
                    success: function(collection, response, options){
                        self.$el.html(self.template({ players: collection.toJSON()}));
                    }
                })              

            }
        });

        return LeaderboardView;
});

哪个渲染正确。由于 fetch 调用是异步的,我假设在 fetch 之后调用模板渲染并不能说明实际存在的数据。进一步的研究表明,这不是在页面加载时填充视图的方法,而是引导它

于 2013-04-14T15:17:27.417 回答