Callback when all Template items finished rendering in Meteor?

前端 未结 7 1009
-上瘾入骨i
-上瘾入骨i 2020-12-19 09:30

I\'m getting a collection of records and placing them in a Template, having them rendered {{#each}} and I want to display a loading icon until the last DOM node

7条回答
  •  情深已故
    2020-12-19 10:14

    There are a few options:

    1. As you mentioned in your final comment, you could create your reactive context and handle changes there.
    2. You could create a special value, ie. _id = "END" and look for that value in the template rendering.
    3. My favorite: instead of using a collection, populate the template as a result of a Meteor.call and Meteor.methods call pair. You could also put the result in a reactive variable so the template automatically re-renders.

    As you may have already discovered, the onReady event of subscribe() fires when the publish method calls ready(), not when all the data has shipped over.

    Here's a simple example of #3 above:

    In the client:

      Meteor.call('get_complete_email',id, function(err,doc) {
        if (err === undefined) {
          // Note current_compose is reactive
          current_compose.set(new _ComposePresenter(action, doc));
        } else {
          log_error('compose','get complete email ',err);
        }
      }); 
    

    In the server:

     Meteor.methods({
       'get_complete_email': function(id) {
         return Emails.findOne({_id:id, user_id:Meteor.userId}, body_message_fields);
       }
     });
    

    In your presenter or viewer code: (the data temp variable could be eliminated - it's legacy and hasn't been refactored out yet).

    Template.compose_to_cc_bcc_subject.prefilled = function(part) {
      if (Current && Current.compose()) { 
        var data = Current.compose();
        if (data == undefined || data[part] == undefined) { return; }
        return data[part]; 
      } else {
        return;
      }
    }  
    

    Obviously, you'll need to wire up current_compose, Current and your own objects a little differently.

提交回复
热议问题