jquery - Meteor: Initing function (carousel) after data is was looped -


i trying make work flickity carousel initing in blaze helpers. have following error:

exception in template helper: typeerror: $(...).flickity not function 

here helper carousel template:

template.carouseltemplate.oncreated(function bodyoncreated() {   this.state = new reactivedict();   meteor.subscribe('albums'); })  template.carouseltemplate.helpers({   albums() {     return albums.find({});   },   initializecarousel () {     $('.carousel').flickity({       // options       "lazyload": true     });   } }); 

and template itself:

<template name='carouseltemplate'>  <div class="carousel">     {{#each albums}}    <div class="carousel-cell">      <img src={{cover}} alt="cat nose" />    </div>    {{/each}}    {{initializecarousel}}  </div>  <template /> 

p.s: open other ways in order make work.

first make sure you're including flickity library using 1 of following options:

1) adding proper js reference application. example:

/client/main.html

<head>   <script src="https://npmcdn.com/flickity@2.0/dist/flickity.pkgd.min.js"></script> </head> 

or

2) embedding copy of flickity library application. store copy of flickity.pkgd.js file in applications /client/compatibility directory (see special directories section of guide more info).

or

3) if using meteor 1.3+, install library using npm: meteor npm install --save flickity

once library installed, instead of initializing carousel through template helper, move initialization code template onrendered callback. within callback wrap flickity initialization in tracker.autorun make sure albums results loaded first. like:

template.carouseltemplate.oncreated(function bodyoncreated() {   this.state = new reactivedict();   meteor.subscribe('albums'); });  template.carouseltemplate.onrendered(function onrendered() {   this.autorun(() => {     if (albums.find().count()) {       $('.carousel').flickity();     }   });   });  template.carouseltemplate.helpers({   albums() {     return albums.find({});   } }); 

calling through onrendered callback means called after template ready , inserted dom (which needed jquery libraries manipulate dom). can remove initializecarousel call template.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -