2016-04-11 14 views
0

Kendi istemci - sunucu uygulamamı oluşturmaya çalışıyorum. Basit bir html iskeleti kurdum ve sunucuya öğe eklemek için alıştırmalardan bazı yöntemleri kullandım. Şu ana kadar veritabanı yok. Hizmetin sınıfından, öğeler eklemek için bu yöntemi var:Öğeleri nasıl düzenler ve silerim?

addItem(name:string, price:number, successCallback, errorCallback) { 
     this.http.post('/item', JSON.stringify({name: name, price: price}), {headers: this.headers}).subscribe((response) => { 
     this.items.push(new Item(+response.text(), name, price)); 
     successCallback(); 
     }, (response) => { 
     errorCallback(); 
     }); 
    } 

sonraki mantıklı adım silme olacağını/düzenleme öğeleri, ancak örnek bu kapağı yoktu, ve muhtemelen yapamam oldukça basit olsa tüm Bunu anlamak veya uygun başka bir örnek bulmak. Burada birinin bana yardım edebileceğini umuyordum. esas olarak uğraştığım şeylerle uğraşıyorum .. sanırım.

cevap

0

Bunun için aşağıdaki yöntemleri uygulamak olabilir:

updateItem(id:string, obj:any) { 
    this.http.put(`/item/${id}`, JSON.stringify(obj), { 
    headers: this.headers 
    }).subscribe((response) => { 
    var itemIndexToUpdate = this.items.find((item => item.id===id)); 
    this.items[itemIndexToUpdate] = obj; 
    }, (response) => { 
    (...) 
    }); 
} 

deleteItem(id:string) { 
    this.http.delete(`/item/${id}`, { 
    headers: this.headers 
    }).subscribe((response) => { 
    var itemIndexToRemove = this.items.findIndex((item => item.id===id)); 
    if (itemIndexToRemove != -1) { 
     this.items.splice(itemIndexToRemove, 1); 
    } 
    }, (response) => { 
    (...) 
    }); 
} 

Aksi takdirde gözlenebilirlerin ziyade addItem yöntemle ancak kaldıraç haline geri aramalar kaldıraç olmaz.

EditRequest(url,data) { 
     this.headers = new Headers(); 
     this.headers.append("Content-Type", 'application/json'); 
     this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token')) 

     this.requestoptions = new RequestOptions({ 
      method: RequestMethod.Put, 
      url: url, 
      headers: this.headers, 
      body: JSON.stringify(data) 
     }) 

     return this.http.request(new Request(this.requestoptions)) 
      .map((res: Response) => { 
       if (res) { 
        return [{ status: res.status, json: res.json() }] 
       } 
      }); 
    } 

DeleteRequest(url,data) { 
    this.headers = new Headers(); 
    this.headers.append("Content-Type", 'application/json'); 
    this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token')) 

    this.requestoptions = new RequestOptions({ 
     method: RequestMethod.Delete, 
     url: url, 
     headers: this.headers, 
     body: JSON.stringify(data) 
    }) 

    return this.http.request(new Request(this.requestoptions)) 
     .map((res: Response) => { 
      if (res) { 
       return [{ status: res.status, json: res.json() }] 
      } 
     }); 
} 

ve ihtiyacı başına istediğiniz yere daha bu abone -

0

bunları dene.

İlgili konular