Here's a way for you to do what you want:
CODE
KONtx.control.SelectButton.implement({
getDisplayValue: function (value) {
value = value || this.getValue();
if (typeof value === "object") {
value = JSON.stringify(value);
}
var label = value;
this.getOptions().forEach(function(option){
var val = (typeof option.value === "object") ? JSON.stringify(option.value) : option.value;
if (val == value) label = option.label;
});
return label;
}
});
Turns out it wasn't quite that easy. The setValue and getValue methods also need to be modified to allow an object as a value.
The original getValue method converts the current value to a String. When a value is an object, we get the string '[Object object]'.
CODE
setValue: function (value) {
var firstblush = this._value == null,
stringvalue = value == null ? "" : String(value);
if ((this._value||"") == stringvalue) return "";
// this._value is set to the string value here, giving us 'Object object]'
this._value = stringvalue;
this.fire( firstblush ? 'onValueInitialized' : 'onValueChanged', { value: stringvalue } );
return this.getValue();
}
Then getValue also converts the value to a String as well, so still the same issue:
CODE
getValue: function () {
return String(this._value||'');
}
What I did, was modify (set|get)Value to leave the value as is. If it's an object then leave it as an object. Only when a comparison needs to be made do I convert everything to a String, or Stringify if it is an object. This appears to be working fine so far. Can you verify the code above appears safe?
CODE
KONtx.control.SelectButton.implement({
getValue : function() {
return this._value || '';
},
setValue : function(value) {
var firstblush = (this._value == null);
var stringvalue = (typeof value === "object" ? JSON.stringify(value) : String(value));
// Same value
if ((this._value||"") == stringvalue) return "";
this._value = value;
this.fire(firstblush ? 'onValueInitialized' : 'onValueChanged', {
value : value
});
return value;
},
getDisplayValue : function(value) {
value = value || this.getValue();
if (typeof value === "object") {
value = JSON.stringify(value);
}
var label = value;
this.getOptions().forEach(function(option) {
var val = (typeof option.value === "object") ? JSON.stringify(option.value) : option.value;
if (val == value) {
label = option.label;
}
});
return label;
}
}