Seth
Seth

Reputation: 363

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.

I have a VectorView. And within that view I have a openlayers featuresAdded event. Within that event I am trying to call another function in the view (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){
}

Upvotes: 1

Views: 278

Answers (1)

jevakallio
jevakallio

Reputation: 35890

The most likely cause of the problem is is that OpenLayers is not setting the correct this context value for the callback function. When you register to the featuresadded event, you need to provide the scope value to events.register.

From OpenLayers documentation:

register: function (type,obj,func,priority)

When the event is triggered, the ‘func’ function will be called, in the context of ‘obj’. Imagine we were to register an event, specifying an OpenLayers.Bounds Object as ‘obj’. When the event is triggered, the context in the callback function will be our Bounds object. This means that within our callback function, we can access the properties and methods of the Bounds object through the “this” variable.

So if you want the this scope to point to your view, you need to pass the view as the second argument. 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);

Upvotes: 1

Related Questions