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

前端之家收集整理的这篇文章主要介绍了javascript – 为嵌套的rest url定义ember数据模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想做一些听起来很简单的事情,但我找不到解决方案.

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

这是我的模型:@H_502_3@

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()
});

路线:@H_502_3@

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.@H_502_3@

问题是,当我想找到文档的页面时,它会调用@H_502_3@

> http://www.myserver.com/api/pages/ID@H_502_3@

代替@H_502_3@

> http://www.myserver.com/api/document/1/pages/ID@H_502_3@

这些嵌套URL在我的应用程序中很重要.@H_502_3@

我在这个主题上找到了不同的东西,比如在JSON响应中添加链接:@H_502_3@

{
    "document": {
        "id": "1","title": "Titre du document","pages": ["1","2","3"],"links": {"pages" : "pages"}
},

但是当我调用这些页面时,它会在没有id的情况下请求http://www.myserver.com/api/document/1/pages.@H_502_3@

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

this.store.find("page",1,{document:1});

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

谢谢.@H_502_3@

解决方法

取决于:EMBER DATA> = V1.0.0-BETA.9

release notes下隐藏了处理嵌套路由的方法@H_502_3@

>需要像这样回复回复链接@H_502_3@

{
    "document": {
    "id": 1,"links": {"pages" : "/documents/1/pages"}
}

>您需要自定义适配器:页面的buldUrl方法@H_502_3@

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相关文章