[英]How do I call a function in my backbone.js view from my Openlayers event in that same view?
Hopefully I am not too confusing here. 希望我在这里不要太困惑。 I have a Backbone.js application and using Openlayers together.
我有一个Backbone.js应用程序,并且同时使用Openlayers。
I have a VectorView. 我有一个VectorView。 And within that view I have a openlayers featuresAdded event.
在该视图中,我有一个openlayers的特征添加事件。 Within that event I am trying to call another function in the view (geoquery).
在那个事件中,我试图在视图中调用另一个函数(geoquery)。 The following below code isn't working.
以下以下代码不起作用。 Any ideas?
有任何想法吗?
//event setup
this.layer.events.on({
"beforefeaturesadded": this.beforeFeaturesAdded,
"featuresadded": this.featuresAdded
});
featuresAdded: function(event){
this.geoquery(polystring);
},
geoquery: function(polystring){
}
The most likely cause of the problem is is that OpenLayers is not setting the correct this
context value for the callback function. 问题的最可能原因是OpenLayers没有为回调函数设置正确的
this
上下文值。 When you register to the featuresadded
event, you need to provide the scope value to events.register
. 当您注册到
featuresadded
事件,您需要提供范围值events.register
。
From OpenLayers documentation : 从OpenLayers 文档中 :
register: function (type,obj,func,priority)
When the event is triggered, the 'func' function will be called, in the context of 'obj'.
触发事件后,将在“ obj”的上下文中调用“ func”功能。 Imagine we were to register an event, specifying an OpenLayers.Bounds Object as 'obj'.
假设我们要注册一个事件,将一个OpenLayers.Bounds对象指定为'obj'。 When the event is triggered, the context in the callback function will be our Bounds object.
当事件被触发时,回调函数中的上下文将成为我们的Bounds对象。 This means that within our callback function, we can access the properties and methods of the Bounds object through the “this” variable.
这意味着在回调函数中,我们可以通过“ this”变量访问Bounds对象的属性和方法。
So if you want the this
scope to point to your view, you need to pass the view as the second argument. 因此,如果您希望
this
范围指向您的视图,则需要将视图作为第二个参数传递。 Assuming you're registering to the event from within the view: 假设您正在从视图中注册事件:
layer.events.register('featuresadded', this, this.featuresAdded);
Or from outside of the view: 或从视图外部:
layer.events.register('featuresadded', view, view.featuresAdded);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.