meteor - Acessing data inside a template at rendering time -
meteor - Acessing data inside a template at rendering time -
i trying create pie chart in meteor can't template see data. code:
piedata = new mongo.collection("piedata") if (meteor.isclient) { meteor.subscribe('piedata'); console.log(piedata.find({}).fetch()); template.piechart.rendered = function () { $('#pie').epoch({ type: 'pie', data: ### info here (a list) ### }); } } if (meteor.isserver) { meteor.publish('piedata', function() { homecoming piedata.find({}); }); }
i tried piedata.find({})
, piedata.find({}).fetch()
returns empty list (same console.log
call). if run piedata.find({}).fetch()
in dev tools console info returned correctly.
the reason why might happing info (this.data) context provided within rendered
function not reactive.
on meteor 0.8.3 ( , maybe other versions) there unofficial way of getting info reactively, have dig meteor's source code find it.
template.piechart.rendered = function () { this.autorun(function () { //this going set dependency var mydata = blaze.getcurrentdata(); //your code goes below } }
use @ own risk.
on meteor 1.0 (as far know) there's official documented way. utilize template.currentdata()
https://docs.meteor.com/#/full/template_currentdata
i used rendered
function create things charts, etc. it's kind of stuff create 1 time got very careful it.
let's in application have list on left , main content area. on list can select different sets of info , when click, main content area gets loaded chart. cool, it's working! wait...
if click on more items on left side list, notice page getting slower. if create chart every single time alter info content, application going suffer , ui gets frozen seconds. in personal experience, has issue simple datetime picker. it's mutual when info context changes dependent functions run more once.
solution:
template.piechart.rendered = function () { this.autorun(function (computation) { //this going set dependency var mydata = template.currentdata() if (computation.firstrun) { //create pie chart, etc. } else{ //update element(s) info create on first run } } }
meteor
Comments
Post a Comment