Meteor: Call function after template is rendered with data

前端 未结 2 1838
暗喜
暗喜 2020-12-11 10:09

I have a number of posts that I want to display inside of a carousel. For the carousel, I use OwlCarousel .

    
2条回答
  •  忘掉有多难
    2020-12-11 10:17

    Its better we call the helper handler from the template exactly after the #each ends other than the onRender.

    As soon as the loop ends and the DOM loads for the same the handler will call the function.

    Like in your case After each place the handler as {{initializeCarousel}}.

    {{#each featuredPosts}} {{! item declaration}} {{/each}} {{initializeCarousel}}
    

    Now define this in the helper as:

    Template.carousel.helpers({ // feed the #each with our cursor
        initializeCarousel: function(){
            $('#featured-carousel').owlCarousel({
                loop:true, autoplay:true, autoplayTimeout:3000, items:1, smartSpeed:1080, padding:80 
            });
        });
    

    This will make the carousel to load once the data is loaded. Hope this will help.

提交回复
热议问题