Currently facing an issue with mapping an http object to a specific model of mine, here is the scenario I am dealing with:
MyObjController.ts
class MyObjController {
constructor (
private myObj:myObjService,
private $scope:angular.IScope,
private modal,
private notifier,
private $translate,
private $state,
public items:MyObjModel[],
public loading
) {
this.loading = this.myObj.all();
this.loading.then((allItems) => {
this.items = allItems;
});
}
}
MyObjService.ts
all () : angular.IPromise<MyObjModel[]> {
let uri = `/myuri`;
return this.api.get(uri).then((response:any) => {
return _.map(response.data, MyObjModel.create);
});
}
MyObjModel.ts
export class MyObjModel {
constructor () { }
id:string;
name:string = '';
organizationId:string = '';
static create (apiResponse) {
let model = new MyObjModel();
model.id = apiResponse.Id;
model.name = apiResponse.Name;
model.organizationId = apiResponse.OrganizationId;
return model;
}
}
The current approach seems problematic as _.map processes each value from response.data independently by creating separate instances of MyObjModel for each property and then forming an array of models. Seeking a solution where the entire response.data can be passed to the create() function to map values to a single instance of the model, returning that singular model instead.
Appreciate any assistance provided!