Launching the function AFTER the meteor pattern is updated

I have a meteor pattern displaying some html that I need to execute a jquery function. Right now, I have configured the dependency, so every time the data (list of objects) attached to this template changes, the function starts. I'm not at all sure that this is the best way to do what I'm trying to do, but it runs the function every time I add / remove / reorder objects to start.

However , the function seems to work before the template is re-rendered, so the previous set of blocks will get jquery-fied, but any blocks that I just added in this action are not.

Is there a way to get the function to run AFTER the template is rendered / updated? Any help is much appreciated!

(a similar question here is Meteorite jet jquery - but it has no answers)

Here are some possibly relevant code bits:

Template.showPictures.rendered = Deps.autorun () -> album = Albums.findOne(Session.get("selectedAlbum")) if (album) pictures = album.orderedPictures() MyApp.loadJQuery() Template.showPictures.helpers pics: -> MyApp.myDependency.depend() album = Albums.findOne(Session.get("selectedAlbum")) album.orderedPictures() 

(Why is autostart in my Template.rendered article? Not sure. It seemed okay, but this is the first time I'm really dealing with dependencies, so I could be completely out of the database. Ideas about what goes wrong will be excellent .)

+6
source share
2 answers

You can use Tracker.afterFlush for this (and Tracker is the new name for Deps ):

 Template.showPictures.rendered = Tracker.autorun () -> album = Albums.findOne(Session.get("selectedAlbum")) if (album) Tracker.afterFlush -> // This will get executed when (among other things) // the UI has been updated. pictures = album.orderedPictures() MyApp.loadJQuery() 

However, the Meteor method is something more:

 <template name="things"> {{#each things}} {{> thing}} {{/each}} </template> <template name="thing"> <!-- Show the thing here... --> </template> 
 Template.things.helpers({ things: function(){ return Things.find() } }) Template.thing.rendered = function(){ // This get executed each time a new thing is rendered! } 
+5
source

I found something that works, but it still seems hacked ...

What I ended up with is setting a timeout. So now, instead of the code above, I:

 Template.sitePreview.rendered = () -> Deps.autorun () -> album = Albums.findOne(Session.get("selectedAlbum")) if (album) pictures = album.orderedPictures() setTimeout MyApp.loadJQuery, 500 

So far, it has been enough time to render / update the template, so jQuery runs in all the latest data. Still hoping for a more elegant solution, but it will be done!

+1
source

Source: https://habr.com/ru/post/982754/


All Articles