Premium support for our pure JavaScript UI components


Post by mamorris »

Hi,

I have a model class that extends Core.data.Model and contains some fields that use dataSource. For this example I have a field called id that has a dataSource of Id.

I create an instance of the model (passing an object that contains an Id property into the constructor). The resulting model has a data property containing Id, as was passed to it originally, and the model itself has a property for id which contains the same value.

If this model is in a store, the toJSON method returns the Id property for the model, rather than the id property, which is not what I expect.
Also, the 'get' function can fetch values for both id and Id. I'd expect it to only fetch a value for id ( get('Id') should be null at this point I think).

My expectation was that the data property would contain the 'id' property, and the 'Id' property would only exist in originalData. I'm especially surprised by the behaviour of toJSON!

Am I misunderstanding dataSource or is it not working properly?

Thanks


Post by Maxim Gorkovsky »

Hello.

First please check sections Defining fields and The idField in this guide: https://bryntum.com/docs/grid/api/Core/data/Model#defining-fields

Basically what you describe here is the expected behavior. Model always has id property, it returns whatever field value that is used as id. You have Id field because this field is created from the incoming data. store.toJSON() is supposed to output objects with mapped fields. Essentially dataSource is a translation rule: we read the value from it when loading data, we put value there when we save the data.

Also, the 'get' function can fetch values for both id and Id. I'd expect it to only fetch a value for id ( get('Id') should be null at this point I think).

This is also an expected behavior, this method will return either a field value, or value from data object, if such field does not exist.

If you have more questions about converting fields, please provide got/expected result and model configuration.


Post Reply