Emberjs: How to get one object from find () (findAll)

I am creating an e-commerce site and have a shopping cart as a model accessible by the web service / api / cart /. Using RESTAdapter, I would just call App.Cart.find() and always return 1 object, not an array. The cart does not have an identifier, so calling App.Cart.find(1) would be wrong - plus this would create a bad web service call: / api / cart / 1.

Do I need to extend RESTAdapter or Model so that find () on App.Cart always returns an object instead of a list?

+4
source share
1 answer
 App.Cart.find().then(function (result) { return result.get('firstObject'); }); 

return the promise to the first object, and then do something with it if it is done. Note that you can use this in the route model method to make it easier to work with promises ember handles.

I used this model. Works great in the latest version of ember.

 model: function(params) { return App.Cart.find({slug: params.slug}).then(function (obj) { return obj.get('firstObject'); }); } 
+6
source

Source: https://habr.com/ru/post/1488336/


All Articles