流星:铁路由器=>没有订阅的waitOn [英] Meteor: iron-router => waitOn without subscribe

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

问题描述

我希望在网站要呈现所有数据之前显示加载模板.

然后在服务器端方法通过Meteor.call给我Data(来自API [异步])之后,我想加载正确的布局.

我尝试了许多在Google上发现的方法,它们描述了相似但并非完全相同的问题.包括使用就绪句柄定义函数的方法,也行不通.我无法运行它.

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

这是我的代码.控制台将2记录在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');
        }
    }
);

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

感谢帮助

解决方案

铁路由器的waitOn不会在Meteor.call()上等待.相反,设置方法是将subscribe设置为waitOn中的记录,而publish是一个包含Meteor.call()的函数,然后为该表创建一个客户端集合.每个用户都会收到呼叫结果.它将类似于以下内容:

客户:

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

我建议看一下

Server Side Method returns an Array.

Thanks for help

解决方案

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:

Client:

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

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

Route:

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

Server:

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.

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

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