I have a situation where I have defined three model classes as 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: the getter
and setter
method of Set
class wouldn't get triggered unless I do new Set()
explicitly (I don't know if the new Set()
this is really necessary).
For example:
"name": "vik",
"containers": [
{
"desc": "something",
"sets": [
{
"dataSet": [
{
"data":[
{
// Object
}]
}]
}]
}]
When i do:
let response:Parent = responseObj;
console.log(response.containers[0].sets[0].data)
it should return the 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 could I improve my code so it works?