2017-07-06 7 views
0

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 
    ) 
    } 

が、私はクライアントを元に戻すために、障害のブロックに何を変更することができます。ここでは、私がこれまでしている何ですか?

+1

オブジェクトをクローンし、必要な変数に設定します。 –

答えて

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呼び出しから設定されているとしてコピーを作成するオブジェクトのクローンを作成しました。元に戻す必要がある場合は元を使用できます。

関連する問題