骨干和下划线模板一个简单的视图
Backbone and underscore - templating a simple view
我在Backbone和underscore中创建的第一个模板视图不太成功。
我从下划线库中得到'players is not defined error'。
这是我的模型:
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数组模板?
您可能希望您的渲染方法看起来更像:
render: function() {
this.$el.html(this.template({ players: this.collection.toJSON() });
}
你需要通过视图的template()函数传递数据上下文,而不是jQuery的html()函数。此外,您希望使用collection. tojson()将集合转换为JSON,而不是使用collection.models.
在文档中挖掘了一些之后,我想到了这个:
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之后调用模板渲染不考虑实际存在的数据。进一步的研究表明,这不是在页面加载时填充视图的方式,而是引导它
相关文章:
- Moment/Jquery-一个简单时间线的愚蠢问题
- 问题用moment JS制作一个简单的时间表
- 测试一个简单的javascript按钮
- 用Jquery map和moment js制作一个简单的时间线
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 调试一个简单的jQuery函数;想知道是否与其他代码冲突
- 一个简单的粘性头jQuery脚本的问题
- Sencha Touch使一个简单的按钮工作
- html表单,它有文本和表,现在我想在一个简单的文本文件中保存和检索数据,如何实现它
- 如何在Javascript中定义一个简单的自动重启倒计时
- 如何使一个简单的HTML文件成为wordpress中可用的页面
- 如何用D3添加一个简单的圆弧
- angularjs做一个简单的倒计时
- 创建一个简单的处理 p5.js 库
- 是否有一个简单的 JQuery 过程来替换页面上每段内部文本的字符串
- 如何在构造函数中创建一个简单的DOM元素,并在调用时将其附加到元素中
- 是否可以将PHP的一个简单片段重新编码为javascript
- 尝试创建一个简单的求和计算器
- 如何使用jquery添加一个简单的转换
- 正在寻找一个简单的JavaScriptHTMLGUI构建器,我可以将其嵌入到我的网站中