模板呈现正常但随后被清除

时间:2013-03-14 18:40:29

标签: javascript 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
        };
    }
});

为什么我会出现这种行为?我做了什么奇怪的事?

1 个答案:

答案 0 :(得分:0)

您不应在Meteor.isClient-check

之外使用Session.set