[英]Meteor.js scope issues
I am doing most of my coding in [appname].js
at the root, and some third party libraries in app/client/compatibility 我的大部分编码都在
[appname].js
的根目录中进行,而一些第三方库则在app / client / compatibility中进行
I am able to load these libraries, and declare and instantiate objects from them in my Template.template1.rendered
handler, which works great. 我能够加载这些库,并在我的
Template.template1.rendered
处理程序中从它们声明和实例化对象,效果很好。
However, when I want to modify one of the variables in Template.template2.events
, specifically the event when a selector is changed. 但是,当我要修改
Template.template2.events
中的变量之一时,尤其是更改选择器时的事件。 When I try to change it from here it tells me my variable is undefined. 当我尝试从此处更改它时,它告诉我我的变量未定义。
I have tried declaring the variable at the top of the file, in isClient
, but that doesn't seem to matter. 我尝试在
isClient
中的文件顶部声明变量,但这似乎无关紧要。 There is no scenario where it is defined there. 没有在此定义的方案。
So my question is how can I modify this variable in my Template.template2.events
? 所以我的问题是如何在
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 the doSomething
function says undefined when called from my change select
event. 的
something
在doSomething
从我的调用时函数说未定义的change select
事件。
I don't believe the issue is one of scope but of context. 我不认为这个问题是范围而是背景。 In your
rendered
function (you should now use the onRendered()
callback) the value of this
is set to the template instance that is being rendered: http://docs.meteor.com/#/full/template_onRendered 在您
rendered
函数中(您现在应该使用onRendered()
回调), this
值设置为正在呈现的模板实例: http : //docs.meteor.com/#/full/template_onRendered
On the other hand, in your template events (and helpers), the value of this
is set to the data context - not the template object. 在另一方面,在你的模板事件(和佣工),值
this
设置为数据上下文- 没有模板对象。 You can, however, access the template instance as this is passed as the second argument to your event map function: http://docs.meteor.com/#/full/eventmaps 但是,您可以访问模板实例,因为它是作为事件映射函数的第二个参数传递的: http : //docs.meteor.com/#/full/eventmaps
It isn't entirely clear what scope you want in your code. 尚不清楚您想要在代码中使用什么范围。 However, your current code includes:
但是,您当前的代码包括:
var imageViewer;
The above is declared in the local file scope (but can be closed over) 以上内容在本地文件范围内声明(但可以关闭)
Template.ocr_results.rendered = function() {
this.imageViewer = thirdParty.Create();
}
The above is declared as a property of the template instance just rendered (an instance of ocr_results). 上面的内容声明为刚刚渲染的模板实例(ocr_results的实例)的属性。
Template.ocr_form.events({
"change select" : function( event ) {
changeImage(this.imageViewer, input );
}
});
The above reference to this.imageViewer
is undefined for two reasons. 上面对
this.imageViewer
引用未定义有两个原因。 First, this
is the data context, not the template instance. 首先,
this
是数据上下文,而不是模板实例。 Second, even if you wrote the below code to use the template instance, it would refer to the ocr_form
template instance, not the ocr_results
template instance (which is where you have defined it in the second block of code above. 其次,即使您编写了以下代码来使用模板实例,它也将引用
ocr_form
模板实例,而不是ocr_results
模板实例(这是您在上面的第二个代码块中定义的位置)。
Template.ocr_form.events({
"change select" : function(event, template) {
changeImage(template.imageViewer, input );
}
});
The above still would be undefined. 上面仍然是不确定的。
As @Curtis has suggested, you should probably remove both your this.
正如@Curtis所建议的,您可能应该同时删除这两个
this.
prefixes to get your code working but creating and using the (closed over) variable just once may not be what you're after - hence the lengthy explanation! 前缀使您的代码正常工作,但是仅创建和使用一次(封闭的)变量可能不是您想要的-因此,冗长的解释!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.