我误解了Session什么是好的选择?

时间:2014-06-16 09:25:09

标签: meteor

目前我正在使用meteor创建应用程序,以便在构建时学习它。我尝试合并Sessions而不是将所有内容写入数据库(我正在做的事情)。在我的理解中,Session是一个存储键值对并具有反应性的全局对象。因此我认为在我的简单游戏中使用模板渲染细节是一个很好的选择。我的目标是小游戏,不同的步骤将根据他们所做的某些动作在每个玩家的模板中呈现。

我重写了我的应用程序,并希望以这种方式使用Session(当然简化)。

我的模板:

<template name="gameRoom">

  <button id='click'>click</button>

  {{#if lastAction}}
    {{>waiting}}
  {{/if}}
</template>



Template.gameRoom.events({
      lastAction: function() {
       return Session.get('lastAction') === Meteor.userId();
    };   
})

Template.gameRoom.helpers({
      'click #click' : function() {
        Session.set('lastAction', Meteor.userId());
    };
})

然而,这并不是我认为可行的方式。看起来每个会话对每个用户来说都是个性化的(当然,考虑到它(替代)替代cookie,这是有道理的。)

所以我的问题是:

  • Sessions有一个很好的选择吗?我真的需要一个 替代或正在使用数据库确定吗?那本地数据库怎么样?

1 个答案:

答案 0 :(得分:1)

您的eventshelpers功能是向后的,您错过了几个花括号,而您的事件键(按钮的ID)是错误的。试试这个:

Template.gameRoom.helpers({
  lastAction: function() {
    return Session.equals('lastAction', Meteor.userId());
  }
});

Template.gameRoom.events({
  'click #click': function() {
    Session.set('lastAction', Meteor.userId());
  }
});

编辑:从你想要做的事情来看,做这样的事情可能是有道理的:

Actions = new Meteor.Collection('actions');

if (Meteor.isClient) {
  Template.gameRoom.events({
    'click #click': function() {
      Actions.insert({userId: Meteor.userId()});
    }
  });

  Template.gameRoom.helpers({
    lastAction: function() {
      var lastAction = Actions.findOne() || {};
      return lastAction.userId === Meteor.userId();
    }
  });
}
相关问题