我有一个client
对象通过内联表单进行更新,如果我的服务无法成功更新客户端,我希望它恢复到客户端更改发生之前的状态。这是我到目前为止所做的:
updateForm() {
let client_id = this.client.id
let curr_client = this.client
console.log("updating", curr_client)
this.clientService.update(this.client, "client", this.token).subscribe(
data => {
// sets this client to new client returned from service
this.client = data
this.clientService.handleResponse("Successfully updated the client!")
this.clientService.setClient(this.client)
},
err => {
console.log(curr_client)
this.clientService.handleResponse("Ut oh.. couldn't update the client!")
// attempt at reverting back but curr_client changes with this.client
this.client = curr_client
},
() => this.editMode = false
)
}
我可以在故障块中更改什么来恢复客户端?
答案 0 :(得分:1)
这就是我所做的:
private currentProduct: IProduct;
private originalProduct: IProduct;
get product(): IProduct {
return this.currentProduct;
}
set product(value: IProduct) {
this.currentProduct = value;
// Clone the object to retain a copy
this.originalProduct = Object.assign({}, value);
}
一旦从Http调用设置了产品,我就克隆该对象以进行复制。如果我需要还原,我可以使用原件。