当前位置: 动力学知识库 > 问答 > 编程问答 >

ember.js - Emberjs: How to get a single object from find() (findAll)

问题描述:

I'm building a e-commerce site and have the shopping cart as a model accessible by the web service /api/cart/. Using the RESTAdapter, I'd like to simply call App.Cart.find() and always return 1 object, not an array. The cart doesn't have an ID, so calling App.Cart.find(1) would be wrong -- plus that would create a bad web service call: /api/cart/1.

Do I need to extend the RESTAdapter or Model to make find() on App.Cart always return an object instead of a list?

网友答案:
App.Cart.find().then(function (result) {
  return result.get('firstObject'); 
});

return a promise to the first object and then do something with that if thats done. Note you can use this in the model method of route to becaulse ember handles promises nicely.

I used in that model. Works perfect in the last ember version.

model: function(params) {
    return App.Cart.find({slug: params.slug}).then(function (obj) {
        return obj.get('firstObject');
    });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: