Callback when all Template items finished rendering in Meteor?

前端 未结 7 1008
-上瘾入骨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条回答
  •  旧时难觅i
    2020-12-19 10:15

    I do it yet another DIY way. Map an index/rank onto your cursor and then check that index against a count of the same query in your .rendered callback:

    items: function() {   
       return Items.find({whatever: whatever}).map(function(item, index) {
          item.rank = index + 1;
          return item;
       });
    }
    
    Template.stuff.rendered = function() { 
      if(this.data.rank === Items.find({whatever: this.data.whatever}).count()) {
         //  Do something cool like initializing a sweet
         //  JS plugin now that ALL your template instances 
         //  are all rendered
         $("#coolplugindiv").coolJSPluginInit();
      }
    }
    

    I'm sure it's slightly more taxing on the server, but it works. Thought I'm curious about template.lastNode and if there's some way to use that to have the same effect.

提交回复
热议问题