Meteor: iron-router => waitOn without subscribe

后端 未结 2 1907
春和景丽
春和景丽 2020-12-18 14:28

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

And after the serverside Method gives me the Data(from an API [async]) via Meteor.ca

2条回答
  •  甜味超标
    2020-12-18 15:13

    I know you've accepted the answer about getting this done via creating a psuedo-publication, but I think there's a more fitting solution.

    The good folks at ars-nebula already did the work to make a Meteor method call wait-able using the https://github.com/arsnebula/reactive-promise/ library.

    Get the promise via:

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

    Then wait on it via

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

    As a shortcut, the Meteor.promise library (https://atmospherejs.com/deanius/promise) can get you a Promise for a method call with merely

    Meteor.promise("methodName", args);
    

    Pubsub is flexible, but reactivity goes beyond that - give this a try and let me know!

提交回复
热议问题