This is a migrated thread and some comments may be shown as answers.

How does the system distinguish between Schema IDs inside JSON Data?

6 Answers 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
NHJ
Top achievements
Rank 1
NHJ asked on 17 Dec 2019, 03:17 PM
Hello,

Let's say you have JSON data created using Read () or some other method.

{userId: testID, userPw: testPW}

It will be in the same form.

Here if i

schema: {
    data: 'list',
    model: {
        id: 'userId',
        fields: {
            userPw: {editable: false}
...

If the schema is defined as above, the key Id is the "userId" field.

But in the JSON data I can see, I don't know which data corresponds to the Key (Schema ID).
(Of course, if you debug your code, you'll know.)

Nevertheless, Key (Schema ID) definitely exists and the system uses it.

So here is the key question. How can users see or access the Key (Schema ID) in their data?


Perhaps this question is the same as asking how the internal logic of the isNew () method accesses a Key in JSON data.


In other words, in the JSON Data object I have

var SchemaID = data. ???;

I would like to know how to extract the Key (Schema ID) with this approach.


(What should be in the ???)

6 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 19 Dec 2019, 11:57 AM

Hi Nam,

The isNew method internally checks whether the id of the current item has a default value. However, we need to know which field of the model is its id, thus, you have to be aware of the id field at the time the grid is being configured.

How to you fetch the data? Do you request some remote service? Do you have the data locally?

Either way, if you request a remote service you will have to request a single element before the grid initialization, find which field is the id and set it in the configuration of the dataSource. If you have the data locally, simply find which is the id field before initializing the grid.

Similar approach is used in the following demo:

Please note that the data is requested, then the model is constructed based on the response and after that the grid is initialized.

I hope this answers your question.

Regards,
Georgi
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
NHJ
Top achievements
Rank 1
answered on 19 Dec 2019, 12:13 PM
Thank you for the answer, but unfortunately it is different from my intention.

I don't want any other way to do it, or how to construct a grid using IDs.

"The isNew method internally checks whether the id of the current item has a default value"

So how does this logic work?

And can the user create that logic?

"However, we need to know which field of the model is its id, thus, you have to be aware of the id field at the time the grid is being configured."

I have set id field. And of course I know the value. (In this case, I mean "person.")

But what a person thinks and what a computer can use is quite different.

Please let me know how I can access and logically use that schema ID field.
0
Georgi
Telerik team
answered on 20 Dec 2019, 01:36 PM

Hi Nam,

The source code of the isNew method is similar to:

isNew: function(){
  this.id === this.defaultId; // whre defaultId is the default value of the type (0 for numbers, null for strings, etc.)
}

Could you please clarify what you mean by `And can the user create that logic?`? Do you want to modify the internals of the isNew method?

Furthermore, I am not quite sure that I understand what you mean by `Please let me know how I can access and logically use that schema ID field.`. Since the developer configures the schema, he should be aware of its value, thus, use it whenever needed. Could you please share some more details regarding the use case?

Regards,
Georgi
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
NHJ
Top achievements
Rank 1
answered on 20 Dec 2019, 03:42 PM

I asked so roughly. Sorry.

I'm writing code to remove a schema ID from a datasource as needed.

Currently I implemented it like this.

If schema id is "userID",

"delete data.userID;"

This code deletes the userID entry from the JSON array, thus losing the key.

But I wrote this code and thought about exceptions.

If the schema ID should be deleted (meaning no key).
What if you still need to use these fields and values?

The explanation of why the schema ID should be deleted is too complicated and the explanation is omitted.

 

But surely the schema ID must be deleted, while the fields and values must be preserved.

In other words, both the userID and userPW fields and values exist on the grid, but the key must be absent.


So here's how I came up with this.

If JSON data
{"key (or id)": "userId"}
 When there was an entry, an attempt was made to delete value or key.

But there was no such data in JSON Data.


Since there was no data I wanted in JSON Data, I thought of another way.

 

Clearly, the key (ID) exists, so we wanted to find a way for the system to check this key internally.

If the key can be accessed in the same way as the system, I thought I could access the key (schema ID) to delete or change the data.

Because again, the system clearly checks if the key exists and one of its methods is isNew ().


Still don't understand this question?

I'll give you an easy example.

int a = 6;

Where is 6 contained? It's just "a" and you need to access "a" when you need a value of 6.

It is like this.

Here "6" is the schema ID and the part I want to access is "a".

I want to know how to access this "a" right away.

I want to know who has the schema id.


Do you understand?
Please let me know if this is possible.

 

0
Accepted
Plamen
Telerik team
answered on 24 Dec 2019, 11:25 AM

Hi,

One way to get the 'id' is to use the idField field of the model. Here is a dojo example based on our GraphQL demo where we log this value in the schema data method when the response just came back. 

You could also review the logic that is actually happening with this field as this code is part of our open source Kendo Core code - https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.data.js#L850

Hope this information will be helpful. If you have further questions please let us know.

Regards,
Plamen
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
NHJ
Top achievements
Rank 1
answered on 25 Dec 2019, 07:21 AM
I wanted this way.

"idField" I wanted to know this method.

Thank you for your help.
Tags
Grid
Asked by
NHJ
Top achievements
Rank 1
Answers by
Georgi
Telerik team
NHJ
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or