【问题标题】:Meteor ReactiveVar doesnt works with Meteor.call in eventMeteor ReactiveVar 在事件中不能与 Meteor.call 一起使用
【发布时间】:2015-05-19 00:51:41
【问题描述】:

我正在使用 ReactiveVar。 onCreated 我设置了 ReactiveVar with Meteor.Call。当我尝试从event 更改我的反应变量时,我得到Exception in delivering result of invoking 'namelist' 希望我下面的代码能更多地解释我的问题。

模板:

   <template name="Name">
    {{#each list}}
      Name : {{name}}
      Age : {{age}}
    {{/each}}
    <button class="loadmore">Load More</button>
    <template>

助手:

Template.Name.helpers({
  'list': function(){
    return Template.instance().list.get();
  }
})

创建时:

Template.Name.onCreated(function () {
  this.limit = new ReactiveVar(5);
  this.skip = new ReactiveVar(0);
  this.list = new ReactiveVar();

  Meteor.call("namelist", Template.instance().skip.get(), Template.instance().limit.get(), function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    } else {
      this.list.set(result);
      this.skip.set(this.limit.get());
      this.limit.set(this.limit.get()+5);
      return;
    }
  }.bind(this));

});

事件:

Template.Name.events({
  'click .loadmore': function(event, template) {

    Meteor.call("namelist", template.skip.get(), template.limit.get(), function(error, result) {
      if(error){
        alert("Oops!!! Something went wrong!");
        return;
      } else {
        temp = template.list.get();
        temp.push(result);
        template.list.set(temp); // Here I am getting error of Exception in delivering result of invoking 'namelist'
        template.skip.set(template.limit.get());
        template.limit.set(template.limit.get()+5);
        return;
      }
    });
  }
});

【问题讨论】:

  • 在你得到事件错误之前,你能告诉我们“temp”是什么样的吗? console.log() 可能。
  • @TimC。感谢您的重播。我解决了这个问题。问题是临时变量不是数组。

标签: meteor meteor-blaze meteor-helper


【解决方案1】:

如果不执行代码,我无法准确判断发生了什么,但可能是您最初的 Meteor.call 没有将 list 设置为数组。此外,temp 声明时没有 var,因此它会泄漏到全局命名空间中。在您的事件处理程序中,您可以尝试编写以下代码:

var temp = template.list.get() || [];
temp.push(result);
template.list.set(temp);

【讨论】:

  • 感谢您的努力。我解决了这个问题。 list 不是数组,我使用 var 声明临时变量:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2017-04-30
相关资源
最近更新 更多