如何使用RESTAdapter处理单个资源 [英] How to handle singular resources with RESTAdapter
问题描述
GET / cart
POST / cart
更新/ b $ b DELETE / cart
ember-data expects find() code>返回一个数组,加上它会自动尝试复制我传递给我的模型的任何url。处理这种情况的最好方法是什么?
你可以在这里做一些事情。
RESTAdapter
调用pluralize,它会在名称的末尾添加一个s,或者在如果存在,则为复数。假设你的DS.Model是 App.Cart
。
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屋!