2014-03-12 27 views
1

我有一條路線/products。在App.ProductsRoute中,我使用setupController掛鉤將從服務器獲取的產品列表分配給本地App.Product對象。如何在使用RESTAdapter使用ember-data時處理emberJs中的HTTP錯誤?

我設置在setupController鉤型號爲:

self_controller.set('model', self_controller.store.find('product')); 

這種運作良好,當HTTP狀態是200。但是,當服務器返回一些HTTP錯誤(如500內部服務器錯誤,401不正當錯誤,等等)我得到錯誤作爲JSON.parse。我不知道如何處理this.store.find()調用的錯誤。

注意:它返回Ember的promiseArray,我需要檢查一次解析(實際分配給模型之前)。任何有關這個話題的幫助將不勝感激。謝謝。

回答

2

如何使用承諾的catch回調來處理錯誤?未經測試,但類似這樣的應該工作:

self_controller.store.find('product').then(function(products) { 
    self_controller.set('model', products); 
}).catch(function(reason) { 
    // Do something to handle the error response... 
}); 
+0

嗨,我可以輸入catch()函數時出現錯誤。但問題是當我調用some_product.save()並得到錯誤時,它仍然將產品存儲在本地的ember-data數組中。我怎樣才能在catch()中取消那個?謝謝。 – Sangram

+1

也許[回滾](http://emberjs.com/api/data/classes/DS.Model.html#method_rollback)? –

+0

是的。我在特定記錄上使用回滾來解決它。非常感謝你的幫助。 – Sangram

相關問題