定义嵌套休息网址的ember数据模型 [英] Define the ember data model for nested rest url

查看:70
本文介绍了定义嵌套休息网址的ember数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



这是我的模型:

  MyApplication.Document = DS.Model.extend({
title:DS.attr('string'),
pages:DS.hasMany('page',{async:true})
});
MyApplication.Page = DS.Model.extend({
文档:DS.belongsTo('document',{async:true}),
标题: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响应中

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

但是当我打电话给页面时,它要求 http://www.myserver.com/api/do



我也尝试在请求页面时指定文档:

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

找不到有关此主题的完整文档,所以如果有人可以解释我有什么问题,我会很高兴。



谢谢。

解决方案

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



处理嵌套路由的方式隐藏在< a href =http://emberjs.com/blog/2014/08/18/ember-data-1-0-beta-9-released.html#toc_better-support-for-nested-records =nofollow >发行说明


  1. 需要以这样的回复回传链接

      {
    document:{
    id:1,
    title:Titre du document ,
    links:{pages:/ documents / 1 / pages}
    }


  2. 您需要自定义适配器:页面 buldUrl 方法像

      MyApplication.PageAdapter = DS.RestAdapter.extend({
    //注意:这是j这是一个简单的例子,但是您可能需要更多的自定义需要
    buildURL:function(type,id,snapshot){
    return'/ documents /'+ snapshot.record.get('document.id ')+'/ pages /'+ id;
    }
    });



I am trying to do something that sounds simple but I can't find the solution.

My application needs to edit documents which contains pages.

Here is my model :

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

And the routes :

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

When I load the document 1, the application call http://www.myserver.com/api/document/1.

The problem is that when I want to find a page of the document, it calls

  • http://www.myserver.com/api/pages/ID

instead of

  • http://www.myserver.com/api/document/1/pages/ID

Theses nested URL are important in my application.

I found different things on the subject like adding links in the JSON response :

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

But when I call for the pages, it requests http://www.myserver.com/api/document/1/pages without the id.

I also try specify the document when I ask for the page :

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

Can't find a complete documentation on this subject, so if someone can explain me what's wrong, I'll be happy.

Thank.

解决方案

Depends : EMBER DATA >= V1.0.0-BETA.9

The way to handle nested routes is hidden under release notes

  1. Need to send back the links with response like this

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

  2. You'll need to customize the adapter:page's buldUrl method like

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

这篇关于定义嵌套休息网址的ember数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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