问题描述:

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');
    });
}
相关阅读:
Top