在父视图中渲染子视图时,我试图解决这个问题。
父视图是一个静态的 html 块,它将是用于过滤子视图的选项卡,子视图是从服务器提取的表格数据。
问题是父视图和子视图渲染正常,但子视图的集合没有数据;但我可以确认该服务正在控制台中返回数据。
这是我的路由器:
define([
'domLib',
'underscore',
'backbone',
'bootstrap',
'view/logged-out',
'view/leaderboard',
'view/leaderboard-filter',
'view/leaderboard-overall',
'view/login',
'view/navigation'
], function($, _, Backbone, Bootstrap, LoggedOutView, LeaderboardView, LeaderboardFilter, OverallLeaderboardView, LoginView, NavigationView) {
var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'leaderboard': 'leaderboard',
'login': 'login'
},
index: function () {
var loggedOutView = new LoggedOutView();
},
leaderboard: function(){
var leaderboardFilter = new LeaderboardFilter();
//var overallLeaderboardView = new OverallLeaderboardView();
},
login: function(){
var loginView = new LoginView();
}
});
var initialize = function(){
var app_router = new AppRouter,
leaderboardView = new LeaderboardView(),
navigationView = new NavigationView();
Backbone.history.start();
};
return {
initialize: initialize
};
});
我的父视图,从路由器调用:
define([
'domLib',
'underscore',
'backbone',
'router',
'view/leaderboard-overall',
'text!template/leaderboard-filter.html'
],
function($, _, Backbone, Router, OverallLeaderboardView, LeaderboardFilterTemplate) {
var LeaderboardFilterView = Backbone.View.extend({
el: 'section[role="main"]',
template: _.template(LeaderboardFilterTemplate),
initialize: function(){
this.innerView = new OverallLeaderboardView();
this.render();
},
render: function(){
this.$el.append(this.template());
this.$el.append(this.innerView.render());
}
});
return LeaderboardFilterView;
});
然后我想在父视图模板 HTML 中呈现一个表格视图:
define([
'domLib',
'underscore',
'backbone',
'router',
'collection/leaderboard-overall',
'text!template/leaderboard-overall.html'
],
function($, _, Backbone, Router, OverallLeaderboardCollection, OverallLeaderboardTemplate) {
var OverallLeaderboardView = Backbone.View.extend({
el: 'section[role="main"]',
template: _.template(OverallLeaderboardTemplate),
initialize: function(){
this.collection = new OverallLeaderboardCollection();
this.collection.bind('reset', _.bind(this.render, this));
this.collection.fetch();
},
render: function(){
console.log('render overall leaderboard');
this.$el.append(this.template({players: this.collection.toJSON()}));
}
});
return OverallLeaderboardView;
});
父模板(希望在 .tab-pane 中呈现子视图):
<nav id="leaderboard-filter">
<ul class="nav nav-tabs">
<li class="active"><a href="#all-time">All Time</a></li>
<li><a href="#today">Today</a></li>
<li><a href="#week">This Week</a></li>
<li><a href="#month">This Month</a></li>
</ul>
</nav>
<section>
<div class="tab-content">
<div class="tab-pane">
<!-- Render leaderboard -->
</div>
</div>
</section>
子视图:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Rank</th>
<th>Player</th>
<th>Games Completed</th>
<th>High Score</th>
<th>Percent Correct</th>
<th>Avg Answer Time</th>
<th>Cumulative Score</th>
</tr>
</thead>
<tfoot>
<!-- Logged in user details here -->
</tfoot>
<tbody>
<% _.each(players, function(player, index){ %>
<tr>
<td><%= index + 1 %></td>
<td><a href="/hyc-web/"><%= _.escape(player.user) %></a></td>
<td><%= player.games_completed %></td>
<td><%= player.high_score %></td>
<td><%= player.percent_correct %></td>
<td><%= player.avg_answer_time %></td>
<td><%= player.cumulative_score %></td>
</tr>
<% }); %>
</tbody>
</table>
两者都按顺序呈现,但子视图不呈现任何数据,只是表头。
无法弄清楚如何让它工作?