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

Custom constructor for Model?

2 Answers 170 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Troy
Top achievements
Rank 1
Troy asked on 12 Jan 2017, 06:19 PM

I'm looking at a case where the "defaultValue" sort of initialization of a Model may not be sufficient.  I wanted to try and hook in to Model's constructor process, but I'm not quite successful.  I have a little example on the dojo that sets up the Model, a DataSource that uses it with a couple rows of data, and a grid that displays it. 

It tries to use the "init" event handler inherited by kendo.data.Model from kendo.Class with my custom constructor function (MyConstructor).  This works in that my constructor function gets called, and I can successfully manipulate the data record (available as "this").  But something about this seems to prevent the datasource from actually finishing up initializing the records with the actual data, even if I make no actual modifications in MyConstructor().  "id" and "description" have their default values, not those from the array of data records that you see when the init constructor does not run.  

I do "kendo.data.Model.call(this)" to run the parent constructor from Model - perhaps something slightly different is needed?   Or is there some other proper way to hook into model construction?  It is for the "create" process on for a grid, so the model instance is created and bound to the edit template all internally in kendo code before I can get ahold of it via the grid's "edit" event.  

2 Answers, 1 is accepted

Sort by
0
Troy
Top achievements
Rank 1
answered on 13 Jan 2017, 09:01 AM

I discovered that the individual Model fields' "defaultValue" specifier can accept a function as well as a value, and this should be enough to handle my problem.  It would still be interesting to know if the "init" constructor approach could in theory be made to work.

The example for defaultValue as a function seems to show the function taking a parameter "e", presumably intended as an event.  It isn't used in the example, and in my initial test the function doesn't seem to get passed any parameters.  Is this a documentation error?  

0
Alex Hajigeorgieva
Telerik team
answered on 16 Jan 2017, 10:02 AM
Hi Troy,

I am glad that you seem to have found a solution for your scenario yourself. I will focus my response on the matter of Kendo UI inheritance. 

To achieve inheritance of the Kendo UI Model class, use the  extend() method:

http://docs.telerik.com/kendo-ui/api/javascript/class#extend

I have modified the provided demo to show you this approach in action as a proof of concept. However, please be advised that custom classes are not subject to the Kendo UI technical support service unless the question or issue can be discussed in the context of the originating class.

The demo I created for you extends the Kendo UI Model class by applying default values to models which have some but not all properties provided. By design, the default values are only applied to an entirely empty object:

https://github.com/telerik/kendo-ui-core/blob/master/src/kendo.data.js#L689

I used both models in two Kendo UI Grids so you may easily compare the different behaviour:

http://dojo.telerik.com/UfUHa

I hope this helps you out.

Kind Regards,
Alex Hajigeorgieva
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
Tags
Data Source
Asked by
Troy
Top achievements
Rank 1
Answers by
Troy
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Share this question
or