为嵌套的rest url定义ember数据模型

tho*_*maf 7 javascript rest ember.js ember-data

我想做一些听起来很简单的事情,但我找不到解决方案.

我的应用程序需要编辑包含页面的文档.

这是我的模型:

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()
});
Run Code Online (Sandbox Code Playgroud)

路线:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

当我加载文档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在我的应用程序中很重要.

我在主题上发现了不同的内容,例如添加linksJSON响应:

{
    "document": {
        "id": "1",
        "title": "Titre du document",
        "pages": ["1", "2", "3"],
        "links": {"pages" : "pages"}
},
Run Code Online (Sandbox Code Playgroud)

但是当我呼叫页面时,它http://www.myserver.com/api/document/1/pages没有id 请求.

当我要求页面时,我也尝试指定文档:

this.store.find("page", 1, {document:1});
Run Code Online (Sandbox Code Playgroud)

找不到关于这个主题的完整文档,所以如果有人能解释我的错误,我会很高兴的.

谢谢.

Ore*_*iya 0

您的问题可能源于 JSON 中 ID 周围的引号。如果您修改序列化程序,以便文档 ID 和页面 ID 周围的 ID 都没有引号,那么您应该会得到您期望的行为。此外,您需要修改链接的格式以指向相对路径:

生成的 JSON 应如下所示:

{
    "document": {
        "id": 1,
        "title": "Titre du document",
        "pages": [1, 2, 3],
        "links": {"pages" : "/documents/1/pages"}
}
Run Code Online (Sandbox Code Playgroud)

请参阅此答案,了解为什么遵守 Ember 对 JSON 格式的期望很重要,并了解预期格式的概述。