If you have an error in one of your computed observable in Knockout.js, the toJson function will set the value of that property to a cryptic script snipped when serializing it to JSON. For example:
The fix is to call the Label function as this.FieldDef().Label() inside SortField's Label.
var SortField = function (expression) {
this.FieldDef = ko.observable(null);
this.SortOrder = ko.observable("");
this.ExpressionType = "SortField";
this.Label = ko.computed(function () {
return this.FieldDef() == null? "" : this.FieldDef().Label + ' ' + this.SortOrder();
}, this);
}
var FieldDefinition = function (table_name, field_name, table_alias) {
this.TableName = table_name;
this.TableAlias = ko.observable(table_alias);
this.FieldName = field_name;
this.Label = ko.computed(function () {
return this.TableAlias() + '⇨' + field_name;
}, this);
In this example SortField’s Label function is not calling the Label function properly from the FieldDef property. If you serialize to JSON, you will get the following:function dependentObservable() {\n if (arguments.length > 0) {\n
if (typeof writeFunction === \"function\") {\n
// Writing a value\n
writeFunction.apply(evaluatorFunctionTarget, arguments);\n
} else {\n
throw new Error(\"Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.\");\n
}\n
return this; // Permits chained assignments\n } else {\n
// Reading the value\n
if (!_hasBeenEvaluated)\n
evaluateImmediate();\n
ko.dependencyDetection.registerDependency(dependentObservable);\n
return _latestValue;\n }\n } ASC"}],"ExpressionType":"Hierarchy","Label":"function dependentObservable() {\n if (arguments.length > 0) {\n
if (typeof writeFunction === \"function\") {\n
// Writing a value\n
writeFunction.apply(evaluatorFunctionTarget, arguments);\n
} else {\n
throw new Error(\"Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.\");\n
}\n
return this; // Permits chained assignments\n } else {\n
// Reading the value\n
if (!_hasBeenEvaluated)\n
evaluateImmediate();\n
ko.dependencyDetection.registerDependency(dependentObservable);\n
return _latestValue;\n }\n }
For the value of the Label property of the SortField.The fix is to call the Label function as this.FieldDef().Label() inside SortField's Label.
Comments