How to attach separate callback property with each entity?

Hi everyone,
I am loading a dataSource from a url. Let’s assume there are 10 entities in the dataSource. Now I want to attach a separate/independent callbackProperty with each entity to change its size. All 10 entities will have different sizes over time.

Currently, this is what I am doing:

for (k = 0; k < entities.length; k++) {

entities[k].point.pixelSize = new Cesium.CallbackProperty( function(time,result)

{

updateSize(entities[k].name, function(size){

sizes[entities[k].name] = size;

});

return sizes[entities[k].name];

}, false);

}

The above code works fine but whenever it changes the size of any entity, it also sets the same size value for all other entities as well as if it’s a same callbackProperty.

I am using “new CallbackProperty()” so why still any change in size for one entity also changes the sizes of other entities?

Any help would be much appreciated. Thanks.

This is a common JavaScript problem, and not something related to Cesium. All the functions refer to the same variable “k”, not the value of “k” at the time the function is created.

This might help:

http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example

Hello,

What is happening in the updateSize function? I don’t see anything specifically incorrect in the code sample you posted.

Best,

Hannah

Hi Hannah,

updateSize() returns the size for the kth entity for the current time which I am storing in an array as a key-value pair <kth entity.name, size>. Later outside the callback I am returning that value by reading it from the array.

So what’s happening is, whenever the size of any entity changes, for some weird reasons, that size also get applied to all the remaining entities.

I wish to attach an independent callback to all the entities i.e. any value change by one entity must not effect the values of others.

Scott, you got it right. All of them were referring to the same variable instead of referring to its value.
Thanks!