I've made a map with cartoDB.js with the createLayer method. I've defined the sublayers,inside a variables called layerSource, giving them an interactivity field like this:
sublayers:[ {sql: "SELECT * FROM db_name",
interactivity: "cartodb_id",
cartocss: "styles"},...more sublayers...
After this I've added the layers to the map
cartodb.createLayer(map,layerSource)
.addTo(map)
.done(function(layer) {
for (var i = 0; i < layer.getSubLayerCount(); i++) {
sublayers[i] = layer.getSubLayer(i);
}
})
.error(function(err) {
console.log("error: " + err);
});
But the sublayers are not interactive. I've seen in the documentation that I have to enable the interactivity with setInteraction:true, but where I should activate this option?? Inside the createLayer (as an argument?), inside the .done??.. I assume this should be something very obvious but I'm new to javaScript and not being able to get this done.
EDIT:
After @Andy_Eschbacher answer.
I'm trying to do what you said but with the first block of code
.done(function(layer) {
for (var i = 0; i < layer.getSubLayerCount(); i++) {
sublayers[i] = layer.getSubLayer(i);
};
sublayer[0].on('featureClick',function(event, latlng, pos, data, layerIndex) {
console.log(data);
});
})
.error(function(err) {
console.log("error: " + err);
});
the console sends an error because sublayer is not defined, so I define a sublayer = layer.getSubLayer(0);
as you do in this tutorial but no action is triggered.
sublayers[0].on(....
(I left out the "s" on sublayers).