带有额外参数的EmberJS RESTAdapter

时间:2014-06-27 10:22:06

标签: asp.net ember.js ember-data ember-cli

我有一个emberjs应用程序,它使用ASP .NET RESTfull Web API从服务器获取数据。我使用ember RESTAdapter连接到web api,这适用于这些场景:

GET    products       //findAll
GET    products/{id}  //find
POST   products       //createRecord
PUT    products/{id}  //updateRecord
DELETE products/{id} //deleteRecord

但是如果我遇到这种情况会怎么样:

GET    products?categoryId=1

如何通过Ember调用此方法并处理结果?

我的ASP.NET REST API控制器

public class ProductsController : ApiController
    {
         private IOrderCaptureRepository _orderCapture;
         public ProductsController()
        {
            _orderCapture = new OrderCaptureRepository();
        }

        public IHttpActionResult Get()
        {
            try
            {
                var result = Wrapper.WrapToEmber(_orderCapture.GetProducts());
                return Ok(result);
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }

        public IHttpActionResult Get(int id)
        {
            try
            {
                var result = Wrapper.WrapToEmber(_orderCapture.GetProduct(id));
                if (result == null)
                {
                    return NotFound();
                }
                return Ok(result);
            }
            catch (Exception e)
            {
                return InternalServerError();
            }
        }

        [HttpGet]
        public IHttpActionResult GetProductsInCategory(int categoryId)
        {
            try
            {
                var result = Wrapper.WrapToEmber(_orderCapture.GetProducts(categoryId));
                return Ok(result);
            }
            catch (Exception e)
            {
                return InternalServerError();
            }
        }
    }

我的适配器:

import DS from 'ember-data';

var ApplicationAdapter = DS.RESTAdapter.extend({
});

ApplicationAdapter.reopen({
    host: 'http://localhost:52703/',
    namespace: 'api/'
});


export default ApplicationAdapter;

1 个答案:

答案 0 :(得分:0)

发布此问题后,我在ember提供的RESTAdapter上发现了findQuery方法。 这解决了我的问题!