vaadin / serverside-elements

Vaadin add-on that makes it easy to use Web Components or any HTML elements from Vaadin Framework
https://vaadin.com/addon/elements-add-on
Apache License 2.0
14 stars 12 forks source link

Arrays containing objects lead to an invalid value on server side #26

Closed stefanuebe closed 4 years ago

stefanuebe commented 4 years ago

Simple use case:

A property on the client side is an array containing multiple objects. This property is synced with the server side on a specific event (using element.bindAttribute). In this constellation the result on the server side is for instance [object Object],[object Object],[object Object],[object Object] instead of [{'id': 1, 'name': 'foo'}, ...].

In general returning an array as a simplified, comma joined string can lead to several problems for instance: what happens, if the string itself also contains a comma or, in my case, that complex structures cannot be transported to the server side.

Request is that there should be a dedicated method like "getAttributeAsJson" that returns the proper json representation to be used in a json parser or that the method handles complext objects / arrays as a special case and returns json for them.

stefanuebe commented 4 years ago

Reason for that seems to be line 36 in elementui.js, where the content of the attribute is directly converted to a string by prefixing it with "" + . Not sure if that is intentional.

A JSON.stringify(...); would maybe be better for the case that the typeof this instance is "object" (and it is not null)?

stefanuebe commented 4 years ago

Possible replacement for line 36:

var valueToSend = ids[id][attribute];
attributeChange = [+id, attribute, valueToSend != null && typeof valueToSend === 'object' ? JSON.stringify(valueToSend) : "" + valueToSend];