Parsing on initialization of kendo.data.Model

3 posts, 0 answers
  1. Jack
    Jack avatar
    100 posts
    Member since:
    Jan 2007

    Posted 14 Nov 2014 Link to this post

    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. Jack
    Jack avatar
    100 posts
    Member since:
    Jan 2007

    Posted 14 Nov 2014 in reply to Jack Link to this post

    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.
  3. Kendo UI is VS 2017 Ready
  4. Kiril Nikolov
    Admin
    Kiril Nikolov avatar
    2564 posts

    Posted 18 Nov 2014 Link to this post

    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!
     
Back to Top