如何在ember js中处理this.store.find('product',id)的空响应?

时间:2014-03-11 07:53:08

标签: ember.js ember-data store promise

在emberjs中,我有/product/:product_id动态路由,可以按预期呈现由productProductRoute的模型挂钩支持的ProductController模板。

this.store.find('product', id)使用RESTAdapter并给我一个响应(遵循RESTAdapter的JSON约定):{product: {"id":"1","name":"prod1"}}并在找到产品时按预期呈现模板。但是当产品为空(在数据库中找不到)时,我得到的响应为:{product: {}}。现在我无法找出拦截响应和检查空dict的方法,并给出正确的警告信息(类似'找不到给定ID的产品')。

注意:promise返回的this.store.find一旦解决就会成为Ember Object of type product

当找不到产品时Ember的错误消息:You must include an id in a hash passed to 'push'

感谢。

1 个答案:

答案 0 :(得分:2)

空请求告诉Ember-Data请求是成功的,即使它不是。当找不到产品时,您的服务器应该使用404响应,而不是空请求。否则,Ember-Data将假设空对象产品。