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

datasource schema parse data is not writable

2 Answers 111 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dean
Top achievements
Rank 1
Dean asked on 22 Mar 2013, 06:38 AM
I am attempting to use the parse function as described in the docs here:
http://docs.kendoui.com/api/framework/datasource#configuration-schema.parse-Function

I'm trying to convert a field in my data to a date like so:

parse: function(data) {
    for(var i = 0; i < data.length; i++){
        data[i].CreatedDate = new Date(data[i].CreatedDate);
    }
    return data;
}

However if I put a breakpoint on the line "return data" and inspect the data object, all of the CreatedDate properties are still strings. I tried various things before checking the property descriptor and finding that writable is false. I checked the property descriptor like so:

Object.getOwnPropertyDescriptor(data[0], "CreatedDate");

and the output was:

{
    configurable: false,
    enumerable :true,
    value: "Mon Oct 13 1975 11:13:00 GMT+1000 (E. Australia Standard Time)",
    writable: false
}

So no matter what changes I tried to make in the parse function, nothing would persist. I even tried data[i].CreatedDate = null so I know it hasn't got anything to do with converting strings to dates.

I created a quick jsbin.com but I couldn't replicate the result (so I don't see a point in including it here)- the property descriptors were writable = true. I'm not sure why the code in my icenium project is causing writeable to be false and I don't even know where to look to try and figure this out. It may have nothing to do with it but my data-source transport is reading from an SQLite database (unlike my jsbin test that used a local array), the code of which is based on the SQLite example project I found in icenium.

Any help would be much appreciated.

2 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 22 Mar 2013, 03:34 PM
Hello Dean,

Thanks for asking this question!
We neglected the way we store date in our sample as we never actually used it.

Working with DATETIME values in SQL seems to be challenging, but actually is not that hard if you follow this rule: Store dates in UTC and display in local format.

Here are three suggestions for your case:

1. (Recommended) First you can time-stamp CreateDate in SQL adding this to your table create statement:
CreateDate TIMESTAMP DEFAULT (datetime('now','utc'))
To format date in SQL SELECT statments use something like this:
SELECT strftime('%Y-%m-%d %H:%M:%S', CreateDate, 'localtime') formattedLocalDate FROM tableWithCreateDate
For reference check SQLite documentation about Date And Time Functions.

2. If you still need to insert or update date via JavaScript the best practice is to use string in ISO format (the ISO 8601 Extended Format) and again format it via SQL in SELECT statments.
JavaScript Date object has a native support for that via .toISOString() method.

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?,?)",
              [todoText, addedOn.toISOString()],
              app.onSuccess,
              app.onError);

3. As for writable: false it is the way WebSQL executeSql returns data objects and it is totally not related to Kendo or Icenium not sure why you jsbin not revealed that. But you can work-around this by simply adding another column to each row on parse: via JavaScript like this:
row.CreateDateFormated = new Date(Date.parse(row.CreateDate))


(Do not forget to DROP and re-CREATE your TABLE.)

Greetings,
Jordan
the Telerik team

Share feedback and vote for features on our Feedback Portal.
Want some Kendo UI online training - head over to Pluralsight.
0
Dean
Top achievements
Rank 1
answered on 25 Mar 2013, 01:46 AM
Thanks Jordan! Using the info you've provided I have managed to get things working.
Tags
General Discussions
Asked by
Dean
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Dean
Top achievements
Rank 1
Share this question
or