我在摆弄 Meteor 并遇到了一些奇怪的行为。我的模板已渲染,但渲染后立即再次清除。
我的模板文件;/client/game.html
<head>
<title>test</title>
</head>
<body>
<h1>test h1</h1>
{{> board}}
</body>
<template name="board">
<table class="gameboard">
{{#each model}}
{{>row}}
{{/each}}
</table>
</template>
<template name="row">
<tr>
{{#each this}}
{{>cell this}}
{{/each}}
</tr>
</template>
<template name="cell">
<td>
{{answer this}}
</td>
</template>
客户档案;/client/client.js
Meteor.startup(function () {
Meteor.call('generateBoard');
});
Template.cell.answer = function (cell) {
console.log(cell.question.answer);
return cell.question.answer;
}
Template.board.model = function () {
//var game = Games.findOne(Session.get("selected"));
var game = Games.findOne(Session.get("selected"));
if(game) {
console.log(game);
return game.board;
}
}
和我的模型文件 /model.js
Games = new Meteor.Collection("games");
Meteor.methods({
generateBoard: function(){
var boardSize = 64;
var rowSize = cellSize = Math.sqrt(boardSize);
var board = [];
var row = [];
for(var rows = 0; rows < rowSize; rows++) {
for (var cells = 0; cells < cellSize; cells++) {
var cell = {
question: Meteor.call("generateAnswer"),
state: 0,
player: null
}
row.push(cell);
}
board.push(row);
row = [];
}
var gameId = Games.insert({
board: board
});
Session.set("selected", gameId);
},
generateAnswer: function(){
var lowerNumber = 2;
var higherNumber = 8;
var firstNumber = Math.round( Math.random() * (higherNumber - lowerNumber) ) + lowerNumber;
var secondNumber = Math.round(Math.random() * (higherNumber - lowerNumber) ) + lowerNumber;
return {
firstNumber: firstNumber,
secondNumber: secondNumber,
answer: firstNumber * secondNumber
};
}
});
为什么我会得到这种行为?我做了什么奇怪的事?