我正在尝试做一些听起来很简单但我找不到解决方案的事情。
我的应用程序需要编辑包含页面的文档。
这是我的模型:
MyApplication.Document = DS.Model.extend({
title: DS.attr('string'),
pages: DS.hasMany('page', {async: true})
});
MyApplication.Page = DS.Model.extend({
document: DS.belongsTo('document', {async: true}),
title: DS.attr('string'),
params: DS.attr(),
objects: DS.attr()
});
还有路线:
MyApplication.Router.map(function () {
this.resource('document', {path: '/document/:document_id'});
});
MyApplication.Document = Ember.Route.extend({
model: function (params) {
return this.store.find('document', params.document_id);
}
});
当我加载文档 1 时,应用程序调用 http://www.myserver.com/api/document/1
。
问题是当我想找到文档的一页时,它调用
http://www.myserver.com/api/pages/ID
代替
http://www.myserver.com/api/document/1/pages/ID
这些嵌套 URL 在我的应用程序中很重要。
我在这个主题上发现了不同的东西,比如在 JSON 响应中添加 links
:
{
"document": {
"id": "1",
"title": "Titre du document",
"pages": ["1", "2", "3"],
"links": {"pages" : "pages"}
},
但是当我调用页面时,它请求 http://www.myserver.com/api/document/1/pages
而没有 id。
我也尝试在请求页面时指定文档:
this.store.find("page", 1, {document:1});
找不到关于这个主题的完整文档,所以如果有人能向我解释问题出在哪里,我会很高兴。
谢谢。
最佳答案
取决于:EMBER 数据 >= V1.0.0-BETA.9
处理嵌套路由的方式隐藏在release notes下
需要像这样回复链接
{ "document": { "id": 1, "title": "Titre du document", "links": {"pages" : "/documents/1/pages"} }
您需要自定义
adapter:page
的buldUrl
方法,例如MyApplication.PageAdapter = DS.RestAdapter.extend({ // NOTE: this is just a simple example, but you might actually need more customization when necessary buildURL: function(type, id, snapshot) { return '/documents/' + snapshot.record.get('document.id') + '/pages/' + id; } });
关于javascript - 为嵌套的 rest url 定义 ember 数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28433942/