不一致的模板实例 - 流星

时间:2015-01-10 10:49:21

标签: javascript html meteor

我有一个game模板,它包含在这样的表中:

<table class="games-table">
   {{#each publisher}}
   <tr id="{{_id}}" class="publisher-row">
      <td class="publisher">
         {{> publisherTemplate}}
      </td>
      <td class="shooter">
         {{#each gamesType '1'}}
         {{> game}}
         {{/each}}
      </td>
      <td class="adventure">
         {{#each gamesType '2'}}
         {{> game}}
         {{/each}}
      </td>
   </tr>
   {{/each}}
</table>

gamesType助手只会在doc中返回相应的游戏(status属性,例如&#34; 1&#34;以及&#34; 2&#34;)。

现在的目标是在表格数据字段之间移动游戏。如果只有一个游戏与gameType相关联(并且在td中),一切正常。但是当存在多个模板实例时,模板实例似乎不一致。

以下是我的模板回调:

Template.game.created = function() {
    console.log("created: " + this.data._id);
};

Template.game.destroyed = function() {
    console.log("destroyed: " + this.data._id);
};

Template.game.rendered = function() {
    console.log("rendered: " + this.data._id);
};

首先渲染控制台输出是这样的(对于两个具有相同gameType(&#34; 2&#34;)的游戏):

created: HN7FL8wKtfxDLzrJM
rendered: HN7FL8wKtfxDLzrJM
created: g2EGnaGW9SP6yhbT7
rendered: g2EGnaGW9SP6yhbT7

这绝对没问题。现在我将status更新为&#34; 1&#34;和控制台输出是这样的(这也是绝对正常的):

created: HN7FL8wKtfxDLzrJM
destroyed: HN7FL8wKtfxDLzrJM
rendered: HN7FL8wKtfxDLzrJM

但现在我将status更新回&#34; 2&#34;游戏再次出现在正确的列中,但请查看控制台输出:

destroyed: HN7FL8wKtfxDLzrJM
created: HN7FL8wKtfxDLzrJM
rendered: g2EGnaGW9SP6yhbT7 

我不知道为什么会发生这种情况而且我真的很沮丧,因为我需要在_id回调中为我的逻辑提供正确的rendered。我试过这个解决方法:

Template.game.created = function() {
    console.log("created: " + this.data._id);
    this.currGame = this.data;
};

...然后使用rendered代替this.currGamethis.data回调中访问它。这种方法的问题在于,这不适用于同一列中的其他游戏(因为this.currGame处于陈旧状态)。

非常感谢任何帮助!

修改 这是gameType助手:

gameType: function(type) {
   return Games.find({publisherId: this._id, status: type});
}

以下是我更新status的方式:

Games.update({_id: "HN7FL8wKtfxDLzrJM"}, {$set: {status: "2"}});

1 个答案:

答案 0 :(得分:0)

好的,我“解决”了我的问题,即使我不明白为什么来自createdrendered的模板实例不同,这是我的解决方法:

  1. 为名为Games
  2. lastUpdated集合添加了新属性
  3. 添加了包Meteor Collection Hooksmeteor add matb33:collection-hooks
  4. 添加了集合挂钩(1)
  5. 更新了gameType帮助(2)
  6. (1)

    Games.before.update(function (userId, doc, fieldNames, modifier, options) {
            modifier.$set = modifier.$set || {};
            modifier.$set.lastUpdated = new Date();
    });
    

    (2)

    gameType: function(type) {
        return Games.find({publisherId: this._id, status: type}, {sort: {lastUpdated: -1}});
    }
    
相关问题