【问题标题】:Meteor: iron-router => waitOn without subscribeMeteor: iron-router => waitOn 没有订阅
【发布时间】:2015-07-07 12:57:45
【问题描述】:

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

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

我尝试了许多在 Google 上找到的描述类似但不完全相同的问题的方法。包括使用准备好的句柄定义函数的方式,也不起作用。我无法让它运行。

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

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

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 返回带有就绪函数的句柄,action 将立即执行。您是否仍有代码返回带有您尝试过的准备好的句柄的函数?
  • No .. 当我这样做并检查操作方法中的 this.ready() 是否始终为真并且它触发/重新运行无限时

标签: meteor loading iron-router


【解决方案1】:

我知道您已经接受了通过创建伪出版物来完成这项工作的答案,但我认为有一个更合适的解决方案。

ars-nebula 的好人已经完成了使用 https://github.com/arsnebula/reactive-promise/ 库进行 Meteor 方法调用 wait-able 的工作。

通过以下方式获得承诺:

var $dep = new $.Deferred();
Meteor.call("myMethod", function(err, result) {
  if (err) { $dep.reject(err); }
  $dep.resolve(result);
});

var promise = $dep.promise();

然后等待通过

Router.route('/route', {
  "loadingTemplate": loading,
  "waitOn": function() {
    return ReactivePromise.when("myTask", $def1.promise(), $def2.promise());
  },
  "action": function() {
    this.render("myTemplate");
  }
});

作为一种快捷方式,Meteor.promise 库 (https://atmospherejs.com/deanius/promise) 可以为您提供一个方法调用的 Promise,只需

Meteor.promise("methodName", args);

Pubsub 很灵活,但反应性不止于此 - 试一试并告诉我!

【讨论】:

    【解决方案2】:

    Iron Router 的waitOn 不会等待Meteor.call()。相反,设置它的方法是将subscribewaitOn 中的记录集,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
      }
    });
    

    我建议看一下Meteor documentation here 中的低级添加/更改/删除发布功能的示例。这是一个密集的部分,但最终包含了使用例正常工作所需的内容。

    【讨论】:

    • 像魅力一样工作.. 只是在 Meteor.subscribe('search', term); 之前添加了 return 语句否则我得到n个异常并且加载钩子没有被触发。非常感谢
    • @JeremyS。你好呀!感谢您发布此解决方案。它非常有帮助!按照您的解决方案,我想在我的Meteor.call() 中加入一个show.modal()。任何建议,将不胜感激。 [链接] (stackoverflow.com/questions/46306445/…)
    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2023-03-12
    相关资源
    最近更新 更多