Run code on client after helpers


#1

How do I initialize a jQuery plugin after helper deliver data for template?

I have the following template:

<div class="scrollyeah">
  {{#each projects}}
    {{!-- some data --}}
  {{/each}}
</div>

and client-side code:

Template.templateName.onRendered(function() {
  $(document).ready(function() {
    $('.scrollyeah').scrollyeah();
  });        
});

Time to time it works, but sometime I have errors like that:

Exception from Tracker recompute function: Error: Failed to execute
’removeChild’ on ‘Node’: The node to be removed is not a child of this
node.


#2

It happed when your template is rendered but your projects isn’t fetched from the subscribtion. You have to wait for all subscribtions ready before initialize jquery plugin.

Template.name.onRendered ->
  this.sub = Meteor.subscribe('projectsList')
  this.autorun =>
    if this.sub.ready()
      this.$('selector').init()

#3

Thanks!
This is exactly the solution I was looking for.


#4

Im having the same problem with owl-carousel
in my case this answer is not helping because my data do not come from a subscription
instead i call it from youtube api, and i need to wait for a json response.
pronosticosCesar: function() { var channel = Session.get('channel'); Meteor.call('pronosticosCesar',channel,function(err,results){ var videos = []; var i=0; arr = $.parseJSON((results.content)); var elementos = arr.items; for(key in elementos) { if(elementos.hasOwnProperty(key)) { var value = elementos[key]; if(value.snippet.title && value.snippet.title !== 'Deleted video') { videos[i] = value; i++; } } } console.log(videos); Session.set('pronosticosCesar',videos); }); //meteor call return Session.get('pronosticosCesar'); }, //pronosticosCesar

then onRendered

Template.Videos.onRendered(function() { $(".owl-carousel").owlCarousel({ itemsCustom : false, itemsDesktop : [1199,4], itemsDesktopSmall : [980,3], itemsTablet: [768,2], itemsTabletSmall: false, itemsMobile : [479,1], items : 4, lazyLoad : true, navigation : true, dotData: false, dots: false }); });
how to wait for a json response? this is not a subscription and my jquery code needs to wait for all the json feed to be ready to initialize or it will start just on some parts of the code