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

Parsing on initialization of kendo.data.Model

2 Answers 107 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 2
Iron
Jack asked on 14 Nov 2014, 09:58 AM
Initialization of kendo.data.model does not parse fields:

var BadModel = kendo.data.Model.define({
        id: 'id',
        fields: {
            id: {
                type: 'string',
            },
            date: {
                type: 'date',
            }
        }
    }),
    now = new Date(),
    badObject = new BadModel({
        id: '1234567890',
        date: now.toISOString()
    });
expect(badObject).to.have.property('date').that.is.a('string');     //Mocha test

but setters parse fields:

 badObject.set('date', now.toISOString());
 expect(badObject).to.have.property('date').that.is.an.instanceof(Date);     //Mocha test

So I have defined a subclassed model as follows:

var FixedModel = Model.define({
    ...
    init: function(data) {
        var that = this;
        $.each(Object.keys(data), function(index, key) {
            if ($.type(that.fields[key]) === 'object') {
                data[key] = that._parse(key, data[key]);
            }
        });
        Model.fn.init.call(that, data);
    }
});

The same applies to Model.accept, which does not parse fields either.

Is this a fix you would not recommend (any side effects)?
Is there an alternative use of the api that should be preferred?
Otherwise, is this something that could be fixed in Kendo UI?

2 Answers, 1 is accepted

Sort by
0
Jack
Top achievements
Rank 2
Iron
answered on 14 Nov 2014, 03:40 PM
There are benefits to parsing on initialization, for example:

var Author = FixedModel.extend({
    id: 'userId',
    fields: {
       userId: { type: 'string', nullable: true },
       name: { type: 'string' }
   }
});
var Book = FixedModel.extend({
    id: 'id',
    fields: {
       id: { type: 'string', nullable: true },
       title: { type: 'string' },
       author: {
           defaultValue: null,
           parse: function(value) {
               return new Author(value);
           }
       }
   }
});
var book = new Book({
    id: 'abcd1234',
    title: 'Les Misérables',
    author: {
        id: 'vh19',
        name: 'Victor Hugo'
    }
});

With the fix suggested above, author is derived from Author, otherwise it is not.
0
Kiril Nikolov
Telerik team
answered on 18 Nov 2014, 11:54 AM
Hi Jacques,

The Kendo UI will not parse the string to a date, when the model is initialized. Data is parsed only when set() method is used, or transport.read is defined - in this both cases the input data will be parsed. In your scenario the string is expected, you can work around this via the init method, as you already did, which is valid solution.

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Jack
Top achievements
Rank 2
Iron
Answers by
Jack
Top achievements
Rank 2
Iron
Kiril Nikolov
Telerik team
Share this question
or