I have a situation where I defined three model classes as shown below
export class Parent
{
name: string;
containers: Container[];
}
export class Container
{
desc: string;
sets: Sets[];
}
export class Sets
{
private _data?: any[];
private _dataSet?: any[];
get data()
{
if (this.dataSet && this.dataSet.length > 0)
{
return this._dataSet[0].data;
}
return this._data;
}
set data(data: any[])
{
this._data = data;
}
get dataSet()
{
return this._dataSet;
}
set dataSet(dataSet: any[])
{
this._dataSet = dataSet;
}
}
The problem is this: the method getterand setterclass Setwill not be called unless I do it explicitly new Set()(I don’t know if it really is new Set()).
For instance:
"name": "vik",
"containers": [
{
"desc": "something",
"sets": [
{
"dataSet": [
{
"data":[
{
}]
}]
}]
}]
When i do:
let response:Parent = responseObj;
console.log(response.containers[0].sets[0].data)
it should return a DataSet value, but instead it returns undefined.
But if I do:
let response:Parent = responseObj;
console.log(new Sets(response.containers[0].sets[0]).data)
returns me the correct value.
Did I miss something or how to improve the code to make it work?
source
share