1

JS API: "changeCustomProperty" notification

Тема: JS API: "changeCustomProperty" notification

Hello,

I need to be notified when a custom property of an unit has been changed. What I've already done:

1) wialon.core.Session.getInstance().loadLibrary("itemCustomFields");
2) included "wialon.item.Item.dataFlag.customFields" into flags
3) unit.addListener('changeCustomProperty', function() { update_unit_model(unit); });

I successfully have got custom fields of units but no notification on change. What else needed to be notified?

Thanks!

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
2

JS API: "changeCustomProperty" notification

Re: JS API: "changeCustomProperty" notification

Hi, thanks for question

Change event triggers for both change and creation, but customFields and customProperties are diffrent things with diffrent data flags, so first of all you need to decide what do you need

Also check your addListener code

// unit in callback is from parent scope, potential bug here
unit.addListener('changeCustomProperty', function() { update_unit_model(unit); });
// same logic, but more safe
unit.addListener('changeCustomProperty', function(event) { update_unit_model(event.getTarget()); });

More info about qx.event.type.Event

3

JS API: "changeCustomProperty" notification

(23/09/2014 15:59:52 отредактировано sergej.shirokov)

Re: JS API: "changeCustomProperty" notification

I definitely need custom fields. So the "changeCustomProperty" is not suitable. Is it possible listen to custom field?

BTW, API documentation for the "changeCustomProperty" mention data represented by {n: name, v: value} hash - sounds like custom field (not property). Bug in the docs?

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
4

JS API: "changeCustomProperty" notification

Re: JS API: "changeCustomProperty" notification

sergej.shirokov пишет:

BTW, API documentation for the "changeCustomProperty" mention data represented by {n: name, v: value} hash - sounds like custom field (not property). Bug in the docs?

From this side admin field, custom field and custom property are equivalent - they all {name: "", value: ""} dictionaries (first two also has id), but they all used for diffrent purposes

sergej.shirokov пишет:

Is it possible listen to custom field?

With Wialon you can almost everything wink

// load library to work with custom fields (including events)
wialon.core.Session.getInstance().loadLibrary("itemCustomFields");
// add "updateCustomField" listener
unit.addListener("updateCustomField", function (e) {
  console.log("Custom field updated", e);
});
5

JS API: "changeCustomProperty" notification

Re: JS API: "changeCustomProperty" notification

Yes, it works. Thank you! But, please, please spend more time on your documentation, which is... not even close to ideal wink

Also, I don't see how the following is "more safe" - it HAVE to be the same unit, right?

>>  // same logic, but more safe
>>  unit.addListener('changeCustomProperty', function(event) { update_unit_model(event.getTarget()); });

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
6

JS API: "changeCustomProperty" notification

Re: JS API: "changeCustomProperty" notification

sergej.shirokov пишет:

Also, I don't see how the following is "more safe" - it HAVE to be the same unit, right?

I havent seen your code, but common practice is to add listener in loop, something like that

for (var i = 0; i < units.length; i++) {
  unit = units[i];
  unit.addListener('changeMessageParams', function handleChangeMessageParams () {
    console.log(unit);
  });
}

The output of this example in my case - one same unit for different events

cg {$$hash: "28-0", $$user_dataFlags: 1048577, _id: 11996104, $$user_name: "TestObject"…} 
cg {$$hash: "28-0", $$user_dataFlags: 1048577, _id: 11996104, $$user_name: "TestObject"…} 
cg {$$hash: "28-0", $$user_dataFlags: 1048577, _id: 11996104, $$user_name: "TestObject"…} 
cg {$$hash: "28-0", $$user_dataFlags: 1048577, _id: 11996104, $$user_name: "TestObject"…} 

Its the way JS works - it binds function to be called (handleChangeMessageParams), not a data inside it.

So when event triggers, handleChangeMessageParams calls and try to operate with unit variable. Since its not declared in function scope - it will be used from parent scope, where it is equal to last element of units array. To prevent this problem any kind of binding should be used.

Using event.target() makes handling event more comfortable - you'll always be able to get item for which event was triggered

Hope this helps

PS once again, i havent seen your code, and maybe you'll never face such a problem, i just noticed potential hole in your example. But event.getTarget() (and whole event object) can be very usefull when dealing with events, so i decided to tell you about it

7

JS API: "changeCustomProperty" notification

Re: JS API: "changeCustomProperty" notification

Oh, now I see what you meant - in my case I have immutable JS closure, so everything is fine. In any case, thanks for the code review! smile

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov