【问题标题】:Questions Meteor about changing view and putting array in collection关于更改视图并将数组放入集合的问题 Meteor
【发布时间】:2013-03-08 02:36:36
【问题描述】:

我有两个关于 Meteor 框架的问题

首先,如何将数组放入 Meteor 集合中?以及如何将值推入其中?

其次,当我有一个按钮并单击它时,如何更改当前视图?这是通过隐藏和显示模板吗?

谢谢!

【问题讨论】:

  • 如果您有两个问题,请发布两个问题。

标签: javascript meteor


【解决方案1】:

使用$addToSet 将值推送到数组中:

var coll = new Meteor.Collection;
coll.insert({myArray: []});
coll.update({}, {$addToSet: {myArray: "myNewValue"}});

有很多方法可以改变视图,但一种简单的方法是使用Session 并检查它是否在您的模板中有值:

<template name="mytemplate">
  <button>Say hello</button>
  {{#if sayHello}}<p>Hello</p>{{/if}}
</template>

Template.mytemplate.events({
  "click button": function() {
    Session.set("sayHello", true);
  }
});

Template.mytemplate.sayHello = function() {
  return Session.equals("sayHello", true);
}

【讨论】:

  • 哈哈,谢谢拉胡尔,真是巧合,我是上周一班上的学生之一。无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 2023-03-26
  • 2014-11-12
  • 1970-01-01
相关资源
最近更新 更多