I'm wondering if there is a way to do this, or if I'm going down the completely wrong path and there's a better way to do this.
So I have some data returned from the server that looks something like this:
[{
fieldId: 12,
name: 'Client',
provider: 'CompanyDataQuery'
},{
fieldId: 11,
name: 'Duration',
provider: null
},{
fieldId: 24,
name: 'Brand',
provider: 'BrandDataQuery'
}]
Now each of these objects needs to have a function attached to it which dictates how a separate set of data should interact with it.
For example I have something like:
{
"$type": "EqualsExpression",
"left": {
"$type": "FieldNode",
"fieldId": 12
},
"right": {
"$type": "CompanyNode",
"value": 829
}
}
I want to look at the 'left' expression and get the value '12', find the corresponding field from the first list, and then ask it for the data.
var field = fields.find(node.left.fieldId);
var displayValue = field.getDisplayValue(node.right.value);
So I want to add a function to the field like:
{
fieldId: 12,
name: 'Client',
provider: 'CompanyDataQuery',
getDisplayValue: function(rightValue) {
// Look up data based on CompanyDataQuery
return companyDataQuery[rightValue].name;
}
}
I know the implementation of these functions. But I'm unsure how to cleanly extend the original object to include this function.
Should I just iterate over these and assign the function as required or create a wrapper for each field... Or is there a better way to do this?
What I want to avoid is creating a giant function full of if statements just to assign or decide which functions to call.