简体   繁体   English

流星:如何遍历数组,这是异步方法的结果

[英]Meteor: how to loop over an array which is the result of an asynchronous method

I have an asynchronous method which does a Facebook request for retrieving photos from the current logged in user. 我有一个异步方法,该方法会执行Facebook请求,以从当前登录用户中检索照片。

I have a template where I loop over these results. 我有一个模板,可以循环显示这些结果。 But since the call to Facebook is asynchronous, it fails. 但是由于对Facebook的调用是异步的,因此失败了。

    <template name="mytemplate">
        {{#if photos}}
            {{> images pics=photos}}
        {{/if}}
    </template> 

    <template name="images">
        {{#with pics}}
            {{#each this}}
                {{images}}
            {{/each}}
        {{/with}}
    </template>

Here is my javascript: 这是我的JavaScript:

      Template.mytemplate.helpers({
      photos: function () {
          return Template.instance().myAsyncValue.get();
      }
  });

  Template.mytemplate.created = function (){
      var self = this;
      self.myAsyncValue = new ReactiveVar("Waiting for response from server...");
      Meteor.call('getPhotos', function (err, data) {
          if (err)
              console.log(err);
          else 
              self.myAsyncValue.set(data.data);
      });
  }  

I already have a variable that is being watched, but the problem is that my template is already created and the result is not an array yet. 我已经有一个正在监视的变量,但是问题是我的模板已经创建,结果还不是数组。

Can someone show me the best practices on how to use an asynchronous array result in a template. 有人可以向我展示有关如何使用异步数组的最佳实践的结果。 I want to create image tags in the template, not with jquery or javascript. 我想在模板中创建图像标签,而不是使用jquery或javascript。

Thanks 谢谢

If I understand this right, you're getting an error when the helpers are loaded because photos is not an array. 如果我理解这一权利,则在加载辅助程序时会出现错误,因为photos不是数组。 Try this: 尝试这个:

  Template.mytemplate.helpers({
  photos: function () {
      var val = Template.instance().myAsyncValue.get();
      return val.constructor === Array && val;
  }

As a side question for you, why are you using a ReactiveVar for an array? 附带的问题是,为什么要对阵列使用ReactiveVar? I have only found a use for using them on primitives, since reactivity doesn't do a deep scan, but maybe I'm missing out :) 我只发现在基元上使用它们的用途,因为反应性不会进行深度扫描,但也许我会漏掉:)

Thanks for the quick response. 感谢您及时的回复。

I've figured out after I wrote the question that my problem was not giving an actual array when the call was not yet made. 在我写了一个问题之后,我想出了一个问题,当调用尚未进行时,我的问题没有给出实际的数组。

Regarding your side question. 关于您的问题。 I have to use this ReactiveVar in order to keep track of the result, no? 我必须使用此ReactiveVar来跟踪结果,不是吗? Because my variable hasn't got a value first. 因为我的变量没有第一个值。 The template is already being setup. 该模板已被设置。 Then when I get a response, the value changed and my each loop reacts over the received array. 然后,当我得到响应时,值改变了,并且我的每个循环对接收到的数组做出反应。 That's how I understood this ReactiveVar anyway :) 无论如何,这就是我对ReactiveVar的理解:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM