Support for associative arrays

Asked by Johan Axelsson

Hi,
I'm trying to read the 'detail' values from a CustomEvent object, but i can't figure out how to read values from an associative array in Cheerp, any hints?

Regards Johan

Question information

Language:
English Edit question
Status:
Solved
For:
Cheerp Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Axelsson
Solved:
Last query:
Last reply:
Revision history for this message
Alessandro Pignotti (a-pignotti) said :
#1

Hi,

could you please elaborate on what do you mean by "associative array"? I see the detail member of CustomEvent just contains the object passed when the custom event is initialized.

Revision history for this message
Johan Axelsson (johan-axelsson) said :
#2

Hi again,
I have created a custom event on a webpage

var myEvent = new CustomEvent("userLogin", {
 detail: {
  mystring: "david",
                                              myint: 1
 }
});

in Cheerp i hava added a evenlistener for that event and want to read the data in the 'detail' fields.

Regards Johan

Revision history for this message
Alessandro Pignotti (a-pignotti) said :
#3

Ok, now I see, thanks for the information.

We plan to support accessing properties using the [] operator in the future, but for the moment you need to declare a corresponding C++ type for the data contained in the JavaScript object. You can do this by declaring the type in the client namespace (see https://github.com/leaningtech/cheerp-wiki/wiki/Browser-side-programming-guide#accessing-the-dom-under-the-hood).

For example:

// Please note that you can add declarations to the client namespace anywhere in your headers or in your cpp files.
// You don't need to modify the system headers provided by cheerp
namespace client
{
    struct MyCustomEventData
    {
          String* get_mystring();
          int get_myint();
     };
}

void eventHandler(client::CustomEvent* ev)
{
    client::MyCustomEventData* data = (client::MyCustomEventData*)ev->get_detail();
    client::console.log(data->get_mystring());
}

Revision history for this message
Johan Axelsson (johan-axelsson) said :
#4

Thanks