流星:铁路由器 =>等待而不订阅 [英] Meteor: iron-router => waitOn without subscribe

查看:30
本文介绍了流星:铁路由器 =>等待而不订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在网站呈现所有数据之前出现一个加载模板.

i want a loading template to appear before the site has all data to be rendered.

在服务器端方法通过 Meteor.call 为我提供数据(来自 API [async])后,我想加载正确的布局.

And after the serverside Method gives me the Data(from an API [async]) via Meteor.call i want to load the correct layout.

我尝试了许多在 Google 上找到的方法,这些方法描述了类似但不完全相同的问题.包括定义具有就绪句柄的函数的方式,也不起作用.我无法让它运行.

I tried many ways found on Google which describe similar but not exatcly the same problem. Including the way to define a function with ready handle, also doesn´t work. I can´t get it running.

我不想使用集合,因为这是特定于用户的数据.(我认为为每个用户[没有登录的用户]创建一个集合是效率不高的,或者我错过了什么)这可能吗?

i don´t want to use Collections because this is user specific Data.( i think this is not efficient to make a collection for each user [no logged in users], or do i miss something) Is this possible?

这是我的代码.控制台在 1 之前记录 2.

Here my Code. The console logs 2 befor 1.

Router.route('/search/:term',{
    name: 'search',
    loadingTemplate: 'loading',
    waitOn : function(){
        var term = this.params.term;
        //i think here has be something differnet either with return subscribe or function with ready-handle
        Meteor.call('search',term,function(err, response) {
            Session.set('shops', response);
            console.log(1);
        });
    },
    action : function(){
        console.log(2);
        this.render();
    }
});

Template.search.helpers(
    {
        "shops" : function(){
            return Session.get('shops');
        }
    }
);

服务器端方法返回一个数组.

Server Side Method returns an Array.

感谢帮助

推荐答案

Iron Router 的 waitOn 不会等待 Meteor.call().相反,设置它的方法是subscribewaitOn 中的记录集,publish 一个包含Meteor 的函数.call(),然后为每个用户创建一个客户端集合来接收调用的结果.它将类似于以下内容:

Iron Router's waitOn will not wait on a Meteor.call(). Instead, the way to set this up is to subscribe to the record set in waitOn, publish a function which contains the Meteor.call(), and then create a client-side collection for each user to receive the results of the call. It will look something like the following:

客户:

// create this collection only on the client
// to receive publication on a per-user basis

Search = new Mongo.Collection('search');

路线:

Router.route('/search/:term',{
  waitOn : function(){
    var term = this.params.term;
    return Meteor.subscribe('search', term);
  }
});

服务器:

Meteor.publish('search', function(term){
  check(term, String);
  var self = this;
  Meteor.call('search', term, function(error, result){
    if (result){
      self.added("search", "mySearch", {results: result});
      self.ready();
    } else {
      self.ready();
    }
  });
});

Meteor.methods({
  search: function(term){
    //do the api call and return it
  }
});

我建议看一下此处为流星文档.这是一个密集的部分,但最终包含了让用例工作所需的内容.

I recommend taking a look at the example of the low-level added/changed/removed publish function in the Meteor documentation here. It is a dense section but ultimately contains what you need to get your use case working.

这篇关于流星:铁路由器 =>等待而不订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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