javascript - Meteor.js scope issues -
i doing of coding in [appname].js
@ root, , third party libraries in app/client/compatibility
i able load these libraries, , declare , instantiate objects them in template.template1.rendered
handler, works great.
however, when want modify 1 of variables in template.template2.events
, event when selector changed. when try change here tells me variable undefined.
i have tried declaring variable @ top of file, in isclient
, doesn't seem matter. there no scenario defined there.
so question how can modify variable in template.template2.events
?
var something; if ( meteor.isclient ) { function dosomething(some, value) { some.property = value; } template.template1.rendered = function() { if(!this._rendered) { this._rendered = true; this.something= thirdparty.create(); dosomething(this.something, document.getelementbyid("text").value); } } template.template2.events({ "change select" : function( event ) { dosomething(this.something, input ); } });
the something
in dosomething
function says undefined when called change select
event.
i don't believe issue 1 of scope of context. in rendered
function (you should use onrendered()
callback) value of this
set template instance being rendered: http://docs.meteor.com/#/full/template_onrendered
on other hand, in template events (and helpers), value of this
set data context - not template object. can, however, access template instance passed second argument event map function: http://docs.meteor.com/#/full/eventmaps
it isn't entirely clear scope want in code. however, current code includes:
var imageviewer;
the above declared in local file scope (but can closed over)
template.ocr_results.rendered = function() { this.imageviewer = thirdparty.create(); }
the above declared property of template instance rendered (an instance of ocr_results).
template.ocr_form.events({ "change select" : function( event ) { changeimage(this.imageviewer, input ); } });
the above reference this.imageviewer
undefined 2 reasons. first, this
data context, not template instance. second, if wrote below code use template instance, refer ocr_form
template instance, not ocr_results
template instance (which have defined in second block of code above.
template.ocr_form.events({ "change select" : function(event, template) { changeimage(template.imageviewer, input ); } });
the above still undefined.
as @curtis has suggested, should remove both this.
prefixes code working creating , using (closed over) variable once may not you're after - hence lengthy explanation!
Comments
Post a Comment