所以我有一堆模板,将{{#each game}}
显示以下模板进行迭代:
<template name="game">
{{#if condition}}
<div class="box">
Page 1
</div>
{{else}}
<div class="box">
Page 2
</div>
{{/if}}
</template>
我想在单击“页面1”框时显示“页面2”,所以我有以下内容:
Template.game.events({
'click .box': function(e) {
Session.set("condition", true);
}
});
但是我不希望所有其他游戏模板都过渡到第2页,而只是过渡到单击的那个。我该如何完成?
编辑:更改应只影响当前用户,而不是所有用户。
假设您的游戏存储在中Meteor.Collection
,并且condition
是文档中的一个属性,该属性应该对所有用户(不仅是当前用户)反映,因此您可以执行以下操作:
Template.game.events({
'click .box': function(event, template) {
Games.update(
{_id: template.data._id},
{$set: {condition: !template.data.condition}}
);
}
});
如果只影响当前用户,则可以使用特定于模板实例的会话变量,并使用一个名为condition
以下函数的帮助器函数将其返回:
Template.game.events({
'click .box': function(event, template) {
Session.set("condition-" + template.data._id, true);
}
});
Template.game.condition = function() {
return Session.get("condition-" + this._id);
};
您可以使用本地集合实现类似的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句