2015-05-18 24 views
0

我正在使用ReactiveVar。 onCreated我设置了ReactiveVar with Meteor.Call。当我尝试从event更改我的无功变量时,我得到Exception in delivering result of invoking 'namelist'希望我的代码能够更好地解释我的问题。Meteor ReactiveVar不适用于Meteor.call事件

模板:

<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(); 
    } 
}) 

OnCreated:

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; 
     } 
    }); 
    } 
}); 
+1

右键,你能告诉我们什么是“临时”的模样? console.log()它可能。 –

+0

@TimC。感谢您的重播。我解决了这个问题。问题是temp变量不是数组。 –

回答

2

没有执行代码,我无法准确判断发生了什么,但可能是因为您的初始Meteor.call未将list设置为数组。此外,temp被声明为没有var,因此它泄漏到全局名称空间中。在事件处理程序,你可以尝试写这样的:你在事件得到错误之前

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

感谢您的努力。我解决了这个问题。 'list'不是一个数组,我用'var'来声明临时变量:) –

相关问题