如何使用RESTAdapter处理单个资源 [英] How to handle singular resources with RESTAdapter

查看:80
本文介绍了如何使用RESTAdapter处理单个资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在垃圾数据中如何处理奇异的资源?说我有以下RESTful路线:

  GET / cart 
POST / cart
更新/ b $ b DELETE / cart

ember-data expects find() code>返回一个数组,加上它会自动尝试复制我传递给我的模型的任何url。处理这种情况的最好方法是什么?

解决方案

你可以在这里做一些事情。

RESTAdapter 调用pluralize,它会在名称的末尾添加一个s,或者在如果存在,则为复数。假设你的DS.Model是 App.Cart



https://github.com/emberjs/data/blob/master/packages/ember -data / lib / adapters / rest_adapter.js#L209

  DS.RESTAdapter.create({
复数:{
cart:'cart'
}
});

如果您的URL方案非常不同,需要更多的逻辑,您实际上可以覆盖buildURL函数。



https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L288

  DS.RestAdapter.create({
buildURL:function(){
return/ always_this
})
});


How are singular resources handled in ember-data? Say I have the following RESTful routes:

GET /cart
POST /cart
UPDATE /cart
DELETE /cart

ember-data expects find() to return an array, plus it automatically tries to pluralize any url I pass to my model. What is the best way to handle this situation?

解决方案

There are a number of things you can do here.

The RESTAdapter calls pluralize, which either adds an "s" to the end of the name, or looks up the name in the plurals hash if it exists. Assuming your DS.Model is App.Cart.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L209

DS.RESTAdapter.create({
  plurals: {
    cart: 'cart'
  }
});

If your URL scheme is very different and requires some further logic, you can actually override the buildURL function.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L288

DS.RestAdapter.create({
  buildURL: function() {
    return "/always_this"
  })
});

这篇关于如何使用RESTAdapter处理单个资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆