流星异步code更新视图 [英] Meteor async code to update a view

查看:90
本文介绍了流星异步code更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我所要做的是使一个HTTP GET请求,然后更新响应的视图。出于某种原因,它不工作。下面是我。

So what I am trying to do is make an HTTP get request and then update a view with the response. For some reason it's not working. Here is what I have.

我一直在关注这个要点: https://gist.github.com/3443021

I have been following this gist: https://gist.github.com/3443021

在客户端:

Template.search.items = function() {
    var query = Session.get("query");
    console.log(query);
    var resp;
    Meteor.call("search", query, function(err, res) {
        console.log(res);
        //return res;
        return [1,2,4];
    });
};

在服务器上:

Meteor.methods({
    search: function(query) {
        var fut = new Future();
        // var onComplete = fut.resolver();

        Meteor.http.get("http://localhost:4242/autocomplete/"+query, function(err, res) {
            var content = res.content;
            var resp = JSON.parse(content);
            console.log(resp);
            fut.ret(resp)
        });
        return fut.wait();
    }
});

和视图上我做的:

<template name="search">
<h1>test</h1>
<table class="table table-hover">
<tbody>
  {{#each items}}
    {{> searchItem}}
  {{/each}}
</tbody>

看来,如果我从Meteor.call功能没有被发送到视图内返回。任何想法?

It seems if I return from inside the Meteor.call function nothing gets sent to the view. Any ideas?

推荐答案

在客户端,没有纤维或任何东西, Meteor.call 是异步的,模板将从助手没有返回值。

In the client, there are no fibers or anything, Meteor.call is asynchronous and the template will get no return value from the helper.

文档

在客户端,如果你不传递一个回调,你是不是末节里面,调用将返回undefined,你将没有办法获得方法的返回值。这是因为客户端不具有纤维,所以没有实际上任何方式,它可以在一个方法的远程执行方框

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method. That is because the client doesn't have fibers, so there is not actually any way it can block on the remote execution of a method.

您可以使用 渲染 回调操作模板它被改变后,手动。

You could use the rendered callback to manipulate the template manually after it was changed.

Template.search.rendered = function() {
  var query = Session.get("query"),
      table = this.find('.table-container');
  console.log(query);
  var resp;
  Meteor.call("search", query, function(err, res) {
    console.log(res);
    table.innerHTML = Template.search_items(res); // render the table with another template
  });
}

这篇关于流星异步code更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆