Meteor模板实例生命周期

Meteor模板实例生命周期,meteor,Meteor,我正在模板实例的数据对象中保存字符串; 字符串在template.rendered上初始化,并可由模板上的控件更新;然后将其提交到将其值保存在集合中的方法: Template.myTemplate.rendered = function() { this.data.myValue = "aaa"; } Template.myTemplate.events({ "click #updateMyValue": function(event, template) {

我正在模板实例的数据对象中保存字符串; 字符串在template.rendered上初始化,并可由模板上的控件更新;然后将其提交到将其值保存在集合中的方法:

Template.myTemplate.rendered = function() {
    this.data.myValue = "aaa";
}

Template.myTemplate.events({
     "click #updateMyValue": function(event, template) {
         if (template.data.myValue = "aaa") template.data.myValue = "bbb";
         else template.data.myValue = "ccc";
     },
     "click #submit": function(event, template) {
         Meteor.call('update', template.data);
     }
});
我第一次点击#提交,一切正常;但随后template.data.myValue变得未定义;单击#submit不会导致页面刷新,因此我希望模板实例会保留其所有数据;
有人能解释“myValue”丢失的原因吗?

不要在
模板上设置属性。数据
,它是包含模板当前数据上下文的Meteor保留属性(只读)

您似乎混淆了
=
(变量赋值)和
==
(比较运算符)

在单页应用程序中,应始终防止表单提交事件的默认行为发生

请尝试以下代码:

Template.myTemplate.onRendered(function() {
  this.myValue = "aaa";
});

Template.myTemplate.events({
  "click #updateMyValue": function(event, template) {
    if (template.myValue == "aaa"){
      template.myValue = "bbb";
    }
    else{
      template.myValue = "ccc";
    }
  },
  "click #submit": function(event, template) {
    event.preventDefault();
    Meteor.call("update", template.myValue);
  }
});