我正在学习canjs并尝试检索数据。因为我是初学者,所以我将整个代码写在一个文件中。
这是我的canjs档案:
Players = can.Control({
init: function(){
this.element.html(can.view('view/players.ejs',{
players: this.options.players
}));
}
})
Player = can.Model({
findAll: 'GET /players'
},{});
var PLAYERS = [
{
"id" : 1,
"name" : "Dipesh",
"rank" : 2,
"score" : 2000,
"__v" : 0
},{
"id" : 2,
"name" : "Aakanksha",
"rank" : 3,
"score" : 3920,
"__v" : 0
}];
can.fixture('GET /players', function(){
return [PLAYERS];
});
$(document).ready(function(){
$.when(Player.findAll()).then(
function(playersResponse){
var players = playersResponse[0]
new Players('.player', {
players: players
});
});
});
这是我的ejs模板:
<ul id="sidebar">
<% list(players, function(player){ %>
<li class="player" <%=(el)-> el.data('player', player) %>>
<%== can.view.render('playerView.ejs', {
player:player
})
%>
</li>
<% }) %>
在运行该文件时,它在jquery中显示错误未捕获的SyntaxError:Unexpected token)。
但怎么可能呢?我在jquery中没有改变任何东西。
答案 0 :(得分:2)
你一定错过了这里的东西
$(document).ready(function(){
$.when(Player.findAll()).then(
function(playersResponse){
var players = playersResponse[0];
// Try to add a semicolon (;) at the end of playersResponse[0]
//Hope this should help you
new Players('.player', {
players: players
});
});
});
更新:尝试在playersResponse [0]的末尾添加分号(;)。希望这对你有帮助。