Why should be used a Schema Model Field Type defined as "object" ( nested or complex type ?? )

12 posts, 0 answers
  1. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 17 May Link to this post

    I cannot understand the following sentence from https://goo.gl/q14C6L very well

    The dataSource is designed to work with flat (not nested) data. Defining a 'complex' objects in the schema.model is not supported

    For this reason I think there is on schema model the from property which allow to rename / alias data fields ( for example from a foreign remote server )

    Yes , but if dataSource doesn't support complex object why :

    1. in the schema.model.<fieldN>.type is present a 'object' type ?
    2. in the grid for example is available dot notation to retrieve in some place a nested value on model ?? es: Category.name - is it not in contrast with previous sentence  ?

    Also - What is the role of field.type == object during various operation ?? for example if I have a datasource with a field of type object in a grid, it is normally rendered like a string. How is it related to defaultValue ??

    So I cannot understand why  documentation is not coherent

     

    Thanks

     

  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1966 posts

    Posted 19 May Link to this post

    Hello,

    This statement is actually true and the DataSource is not designed to work with complex objects internally. The reason why there is an type object in the schema.model is because JavaScript data types includes object. Indeed you are able to use dot notation and having nested field (since it is JavaScript after all), but the Kendo UI DataSource is designed (internally) to expect and work only with flat data. 

    The default value should be set in the schema.model configuration as demonstrated in the Grid / Editing custom editor demo with the Category field. 

    Regards,
    Boyan Dimitrov
    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 visualization (charts) and form elements.
  3. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 28 Sep in reply to Boyan Dimitrov Link to this post

    Thanks for your answer, I also noticed that the default type is not string:

    options.fields.fieldName.type String
    Specifies the type of the field. The available options are "string", "number", "boolean", "date" and "object". The default is "string".

     

    See following PHP code , we have dynamic model fields :

     

    foreach ($activities as $key => $activity){
                $this
                    ->addField((new DataSourceSchemaModelField('activity_'.$activity->id))
                        ->type('object')
                        ->editable(true)      
                        ->defaultValue([
                            'priority' => 0,
                            'member' => null,
                            'status' => null
                        ])
     
                    );
            }

     

    I have a complex type ( object ) for these model fields, as you can see in default value

    var serverResponse = {
        data: [{
            activity_1: {
                member: 2, member_name: "AMatteo", status: null, status_color: null, priority: 0
            },
            activity_6: {
                member: 1, member_name: "Paolo", status: 3, status_color: "#ff7f27", priority: 0
            },
            activity_7: {
                member: 1, member_name: "Paolo", status: null, status_color: null, priority: 0
            },
            activity_9: {
                member: null, member_name: null, status: 3, status_color: "#ff7f27", priority: 0
            },
            description: "asdfadf",
            id: 1,
            note: "*",
            project_details_id: 1
        }],
        total: 1
    };

     

    also I have a custom template editor see attached image , my app is correctly working now , but I cannot match the app behaviour with the initial statement about type default :

    1. type == 'object' : all works as expected, data from server is left untouched and I can correctly serialize it thanks to multiple data-bind value in template editor ( the last in the post )

      activity_1[member]:2
      activity_1[member_name]:AMatteo
      activity_1[status]:
      activity_1[status_color]:
      activity_1[priority]:0
      activity_6[member]:1
      activity_6[member_name]:
      activity_6[status]:3
      activity_6[status_color]:#ff7f27
      activity_6[priority]:0
    2. remove any type attribute : same as point 1 , all is working , but the doc says the default is string ....
    3. let's explicit type == 'string' : now the code stop working, I did't debug but the behaviour between type == string and type not specified is different !! is the doc wrong ?

    Thanks !

    <script type="text/x-kendo-template" id="activity-preview-template">
            <div # if(color) { # data-color="#: color #" # } # style="position: relative;">
                #: text #
            </div>
            <div class="statusbox hide">
                  
            </div>
            # if (priority!=0) { #
            <div class="priority"> </div>
            # } #
        </script>
        <script type="text/x-kendo-template" id="cell-template">
            <ul class="fieldlist">
                <li>
                    <div>
                        <input tabindex="#: tabindex #" name="#: field #_member" data-bind='value: #: field #.member'
                               style="width: calc(100% - 28px); "></input>
                          <span class="k-icon k-i-user"> </span>
                    </div>
                </li>
                <li>
                    <div class="">
                        <input tabindex="#: tabindex + tabMax #" name="#: field #_status" data-bind='value: #: field #.status'></input>
                          <span class="k-icon k-i-clock"> </span>
                    </div>
                </li>
                <li>
                    <div class="">
                        <input tabindex="#: tabindex + tabMax * 2 #" name="#: field #_priority" min="0"
                               data-bind='value: #: field #.priority'></input>
                          <span class="k-icon k-i-warning .k-i-exception .k-i-error"> </span>
                    </div>
                </li>
            </ul>
        </script>
  4. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1966 posts

    Posted 02 Oct Link to this post

    Hello Paolo,

    I am not sure that I fully understand the question here. Could you please send us a sample dojo that replicates the problem and provide some additional explanation? 

    Regards,
    Boyan Dimitrov
    Progress Telerik
    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 visualization (charts) and form elements.
  5. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 04 Oct in reply to Boyan Dimitrov Link to this post

    Hi, thanks, I can replicate but it needs a long time, my exact question is related to this sentence :

    options.fields.fieldName.type String
    Specifies the type of the field. The available options are "string", "number", "boolean", "date" and "object". The default is "string".

    I supposed default type is "string" , so I wondered , which type would I have to choose for a complex data type (we have a grid cell custom editor template, see previous server response to understand, everty activity_N field i edited via cell custom editor in the image) ?

    So i tried different values in schema model field type: string, object and 'no type non specified' , I supposed string and  'no type non specified' will behave in the same way, because the doc says the default is 'string', but it behaves differently

     

    It seems my code works also with type not specified, instead if I put string it stop working. it is right that stop working with string, but it would stop working also when I dont explicitely specify type ...

     

    Thanks

     

  6. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1966 posts

    Posted 06 Oct Link to this post

    Hello Paolo,

    I believe that Use Nested Model Properties can help you out to understand how nested model fields can be used with reading and editing operation. 

    Regards,
    Boyan Dimitrov
    Progress Telerik
    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 visualization (charts) and form elements.
  7. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 09 Oct in reply to Boyan Dimitrov Link to this post

    Thanks you Boyan, I didn't find that doc page before, I read carefully it !

    My only question is the initial question :

    When should be used a Schema Model Field Type defined as "object" ? 

    I cannot find any examples about it and the doc says the default is string when it is left empty, and in your previous example we cannot find any presence of type == object

     

    Thanks for your patience

  8. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 09 Oct in reply to Paolo Link to this post

    Hi

    I definitely look  into the code , and now I can understand :

    1. the doc says the wrong thing! the default is not string !!!! but 'default' , see the code, only default leaves the data untouched
    2. string correctly does a object cast, and in my case breaks the code !! so I cannot use string but I can left it empty because string is not the default !!!
    3. there is not presence of string 'object' in your code but only 'default' ! so it should be rectified 
    var parsers = {
               'number': function (value) {
                   return kendo.parseFloat(value);
               },
               'date': function (value) {
                   return kendo.parseDate(value);
               },
               'boolean': function (value) {
                   if (typeof value === STRING) {
                       return value.toLowerCase() === 'true';
                   }
                   return value != null ? !!value : value;
               },
               'string': function (value) {
                   return value != null ? value + '' : value;
               },
               'default': function (value) {
                   return value;
               }
           };

     

    Model.define = function() {
        //....
       for (name in proto.fields) {
           field = proto.fields[name];
           type = field.type || 'default';
           // ..... My statement is confirmed ! type == 'object' has no meaning !!!
       }
    }

     

     

  9. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 10 Oct in reply to Paolo Link to this post

    Hi

    I revised also the kendo editors generator in particular fieldType method :

    fieldType = function(field) {
            field = field != null ? field : '';
            return field.type || $.type(field) || 'string';
        }

     

    fieldType is used in many place to determine the field model type.

    There we can see the default editor for model field with empty type property is 'string' so when a model field has no type the editor rendered is of string type. So I can further clarify :

    1. for models without a type property the default parser choosed is 'default' and not 'string' , no cast is done !!
    2. for models without a type property the default editor is 'string' 

    I think this behaviour could be documented more deeply even if I think is conflicting ( default parser I think should adhere to default editor used ) 

    at first I cannot truly understand it without code inspection

  10. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    300 posts

    Posted 11 Oct Link to this post

    Hi, Paolo,

    My name is Alex and I am taking over the thread from my colleague Boyan.

    The Kendo UI Data Source Schema Model definition is important for the editing functionality of the Kendo UI Grid to work correctly. I believe the reason why we have reflected that the default type is a string is to inform our clients that if a type is not specified, the editor which will be generated is one for string type:

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

    editor = editor ? editor : editors.string;

    In addition to that, you can see that the default value for the "default" type is indeed empty string:

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

    var defaultValues = {
      "string": "",
      "number": 0,
      "date": new Date(),
      "boolean": false,
      "default": ""
    };

    I also inspected the schema model fields serialization for MVC and the object types are serialized as "object":



    I am wondering if what you are experiencing is anything specific to the PHP wrappers. If you can provide us with instructions how to reproduce the erroneous behaviour, we will be glad to investigate and amend the PHP data source model serialization as required.

    Look forward to hearing back from you.

    Kind Regards,
    Alex Hajigeorgieva
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  11. Paolo
    Paolo avatar
    11 posts
    Member since:
    Mar 2017

    Posted 11 Oct in reply to Alex Hajigeorgieva Link to this post

    Hi Alex, thanks for joining the discussion, I will try to answer you more accurately

    I needed in a situation to undestand which was the default parser and which the default editor, as you said I realized the model default is linked to the editor behaviour  , but in my app I have a custom editor that edits a property on model that is an object

    {
       // grid columns edited by a custom editor match with an object model field activity_1
       activity_1: {
           member: 2, member_name: "AMatteo", status: null, status_color: null, priority: 0
       }
       // ....
    }

     

    so in my app I used on model type = 'object', as the doc says , to stop any string cast on server data ( even if in the code object does nothing !! )

    I was only disappointed when I unintentionally removed the type properties on model, It didn't stop working !! I expected it should have stop working because the default is string and I thought the parsers should cast it to string

    Reading the code I understood that in my case it is better leave the model type empty because it does the trick:

    1. this stop parsers to string cast on server data ( contrasting to the custom editor side which says string is default )
    2. I have not a default editor and I don't need to use type == 'string' , but leaving it empty I achieve my goal at the same time

    In my scenario specifyng type == 'string' make app stop working because activity_1 is string casted and my custom editor cannot works anymore

  12. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    300 posts

    Posted 13 Oct Link to this post

    Hi, Paolo,

    I believe I understand your confusion and I will consult with the team for a better phrasing of this portion of our documentation. As a token of appreciation for helping us improve, I have also added some Telerik points to your account.

    Now, I would like to shed some light why the Kendo UI Grid continues working.

    The Kendo UI Grid continues working because in your case when there is a custom editor, the custom editor is used instead of the built-in ones. Since setting the type is optional, the type is checked internally, too.  The presence of the custom editor and the underlying object type evaluate this code to true:

    isObject = isPlainObject(field), // this is the jQuery isPlainObject method
    isCustomEditor = isObject && field.editor,  // the existing custom editor
    editor = isCustomEditor ? field.editor : editors[type],  // the field editor becomes the editor in the application

    I debugged one of our demos which uses an object for one of its fields. Removing the type declaration and custom editor,  the default editor for a string is created (an input):





    So I suppose that our documentation should remove the object type from the possible options and add that if no type is defined AND no editor, the Kendo UI Grid will generate an input(string editor). Would that be better? What do you think?

    Kind Regards,
    Alex Hajigeorgieva
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top