meteor - MeteorJS Blaze.getData() occasionally returns undefined -
i'm rendering bootstrap modals on webpage using meteorjs's "renderwithdata" method load each template when it's needed.
i'm running issue helper methods access data in modal using "blaze.getdata()" return undefined , i'm unsure how fix that.
the way i've been able replicate issue creating/destroying modals , there doesn't seem causes issue.
here steps i've been taking:
1) instantiate modal proper data
template.courses.events({ 'click .share-course': function (e,t) { var courseid = $(e.target).data('courseid'); template.instance().activecourse.set( createmodalwithdata( { currentinstance: template.instance().activecourse.get(), template: template.enrollment_generator, datatorender: {courseid: courseid} } )); $('#generateenrollmenturl').modal('show'); } });
also, here code "createmodalwithdata":
// create modal specific data context // if modal template exists, destroy // , re-create new data context. // if location render isn't specified, renders // content in body . // parameters: [object] data { currentinstance : template || null, // template : template, // datatorender : object, // (optional) location : element // return: blaze template instance createmodalwithdata = function createmodalwithdata(data) { // ensure data exists if (_.isundefined(data) || _.isnull(data)) { throw "data cannot null or undefined"; } // if modal exists, destroy if (!_.isnull(data.currentinstance)) { blaze.remove(data.currentinstance); } // if location undefined, set page body if (_.isundefined(data.location)) { data.location = document.body; } // render modal datatorender return blaze.renderwithdata(data.template, data.datatorender, data.location ); };
2) attempt retrieve data using "blaze.getdata()" within modal template
template.enrollment_generator.oncreated(function() { var courseid = blaze.getdata().courseid; // undefined meteor.subscribe('enrollment-codes',courseid); });
so far i've attempted replace "oncreated" method "onrendered" still had same issue.
it turns out issue within click event. had nested span element within share-course button:
<small class="share-course" data-courseid="{{_id}}"> share <span class="glyphicon glyphicon-share"></span> </small>
this messing way targeting embedded courseid
instead of blaze.getdata(), should have been using template.currentdata() retrieve data within template
as stated here: https://forums.meteor.com/t/blaze-getdata-question/5688
Comments
Post a Comment