Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo MVVM binding on null values binds as object

there seems to be a rather peculiar design decision when it comes to the binding behaviour in Kendo.

I'm trying to bind a nullable value to a drop down list. If the (remotely sourced) value is null, then Kendo will generate objects to bind to the value instead, for example, using the data-value-field/value of the dropdown. This makes saving the modified viewmodel extremely unreliable, since now entire objects would get transmitted instead of simple data types.

Example: Schedule.WeekNumber is null. After selecting a value from a DropDown like

<select data-role="dropdownlist" name="scheduleWeekOfMonth"
    data-bind="value:WeekNumber">
  <option value="">Every week</option>
  <option value="0">First week of month (1.-7.)</option>
  <option value="1">Second week of month (8.-14.)</option>
  <option value="2">Third week of month (15.-21.)</option>
  <option value="3">Last week of month (22.+)</option>
</select>

The value (JSON) then is Object {text: "First week of month (1.-7.)", value: "0"} instead of "0"

Has anyone got a good solution for this? - besides replacing all null values of the viewmodel on load, eg:

function deNull(obj) {
  if(obj !== null && typeof obj === "object") {
    $.each(obj, function(index, value) {
      if(value !== null && typeof value === "object") {
        deNull(obj[index]); // we must go deeper (BRRRRRRRAAAAAWWWWRWRRRMRMRMMRMRMMMMM)
      }
      if(value === null) {
        obj[index] = "";  // workaround, this is the "null" equivalent of not being set         
        //console.log("De-nulled: " + index);
      }
    });
  }
}
like image 430
urbanhusky Avatar asked Oct 04 '22 09:10

urbanhusky


1 Answers

There is a data-value-primitive attribute you can use to achieve straightforward value binding.

<select data-role="dropdownlist" name="scheduleWeekOfMonth"
  data-bind="value:WeekNumber" data-value-primitive="true">
    <option value="">Every week</option>
    ...
</select>

Here is a link to in the docs about this attribute. Also, there is a UserVoice suggestion.

like image 98
zacharydl Avatar answered Oct 13 '22 10:10

zacharydl