简体   繁体   English

如何在同一视图中的Openlayers事件中在bone.js视图中调用函数?

[英]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.

相关问题 从ribs.js视图并使用Openlayers,我将如何与openlayers事件中的地图进行交互? - From a backbone.js view and using Openlayers, how would i interact with the map from a openlayers event? 在Backbone.js中如何从另一个视图调用一个视图中的函数? - In Backbone.js how do i call a function in one view from another view? 我如何在ID的click事件上在视图上调用函数,函数是写在骨架中的controller.js中的 - How can i call function on view on the click event of Id, function is written on controller in backbone.js Backbone.js:如何确定事件后父视图的模型? - Backbone.js: How do I identify the model of the parent view upon an event? 如何在触发插件事件时从Backbone.js中的View执行Model函数? - How to execute a Model's function from a View in Backbone.js when a plugin event is triggered? Backbone.js - 给定一个元素,我如何获得视图? - Backbone.js - Given an element, how do I get the view? 如何销毁此Backbone.js View实例? - How do I destroy this Backbone.js View instance? 为什么当我单击X时,所有项目都已从Backbone.js视图中删除 - Why are all item being removed from my Backbone.js view when I click the X 视图内部的视图中的Backbone.js触发事件 - Backbone.js firing event from a view inside a view 呈现lobb.js集合视图时遇到问题。 在我的初始化函数中得到“ Uncaught TypeError:无法调用未定义的方法'on'” - Having trouble rendering backbone.js collection view. Getting “Uncaught TypeError: Cannot call method 'on' of undefined” in my initialize function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM