If the value or text properties of the data items used by Kendo DropDownList are defined as getters and valuePrimitive is set to true the following errors occur:
The following error is thrown (see console):
Error: Expected value of primitive type. See http://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist/#toc-value-selection
the ngModel becomes the data item instead of the primitive value
After selection of an item '[object Object]' is displayed as text
Note: the the example project on StackBlitz works if valuePrimitiveis set to true.
The cause for the error is in the function DropDownListComponent.prototype.propof dropdownlist.component.js:
if (usePrimitive) {
return field && dataItem.hasOwnProperty(field) ? dataItem[field] : dataItem;
}
If field is a getter which is defined on the object's prototype that represents the class then hasOwnProperty() returns false although field refers to a valid (logical) property.
To bypass the error in the example project above you can patch hasOwnProperty() like this:
Size.prototype.hasOwnProperty = function(p) {
if (p === 'value' || p === 'text') {
return true;
}
return Object.prototype.hasOwnProperty.call(this, p);
}
This resolves all the issues described above.
A correct check for logical properties needs to check for Object.getOwnPropertyDescriptor(Object.getPrototypeOf(dataItem), field).get on the entire prototype chain. Maybe the existence check in DropDownListComponent.prototype.prop should be removed. I do not see any benefit in working around a potential misconfiguration of text resp. value fields that also introduces this kind of issues.
I'm submitting a...
Current behavior
If the
value
ortext
properties of the data items used by Kendo DropDownList are defined as getters andvaluePrimitive
is set totrue
the following errors occur:Error: Expected value of primitive type. See http://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist/#toc-value-selection
ngModel
becomes the data item instead of the primitive value'[object Object]'
is displayed as textThis can be reproduced with the following project on StackBlitz: https://stackblitz.com/edit/angular-uzrpnv
Code:
Expected behavior
ngModel
should always reflect the primitive valuelabel
field of the item should be displayed as textMinimal reproduction of the problem with instructions
Open project on StackBlitz and select an option.
Environment
Package versions:
Browser:
System:
Additional information
Note: the the example project on StackBlitz works if
valuePrimitive
is set totrue
.The cause for the error is in the function
DropDownListComponent.prototype.prop
ofdropdownlist.component.js
:If
field
is a getter which is defined on the object's prototype that represents the class thenhasOwnProperty()
returnsfalse
althoughfield
refers to a valid (logical) property.To bypass the error in the example project above you can patch
hasOwnProperty()
like this:This resolves all the issues described above.
A correct check for logical properties needs to check for
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(dataItem), field).get
on the entire prototype chain. Maybe the existence check inDropDownListComponent.prototype.prop
should be removed. I do not see any benefit in working around a potential misconfiguration oftext
resp.value
fields that also introduces this kind of issues.