简体   繁体   English

如何正确设置具有hasMany关系的余烬数据的ajax请求?

[英]how to setup an ajax-request for ember-data with hasMany-relationship correctly?

after i full day of trying i give up. 经过一整天的尝试,我放弃了。 for a very basic application in ember.js i tried to setup an ajax request to the server that answers with a proper reply. 对于ember.js中一个非常基本的应用程序,我尝试向服务器设置ajax请求,并以正确的答复进行回答。 the problem is not to get the model data (custom_route) from the server showing up in an array controller's template. 问题不在于从服务器获取模型数据(custom_route),该服务器显示在阵列控制器的模板中。 but what i couldn't manage to fetch is the data of the related model (station). 但是我无法获取的是相关模型(工作站)的数据。 The both models are definded as followed: 两种模型的定义如下:

App.CustomRoute = DS.Model.extend({
  name: attr('string'),
  description: attr('string'),
  stations: DS.hasMany(App.Station)
});

App.Station = DS.Model.extend({
        name: attr('string'),
        description: attr('string'),
        stations: DS.hasMany(App.Station)
    });

Using the FixtureAdapter everything works perfectly fine after adding {async: true} as parameter to both methods. 使用FixtureAdapter,在将{async:true}作为参数添加到这两种方法之后,一切工作都很好。 But with the default RESTful adapter the only thing i get displayed is the data from the custom_route model. 但是使用默认的RESTful适配器,我唯一显示的是来自custom_route模型的数据。 That leads me to my question: How has the ajax reply to look like that the adapter fetches both the data from both models. 这引出了我的问题:ajax回复看起来像适配器从两个模型中获取两个数据一样。 On the net I couldnt find an example of how a reply to a findAll() request that resolves a model and its related models. 在网上,我找不到一个示例,说明如何回复findAll()请求以解析模型及其相关模型。 Everything I found only examplifies how the reply should look like (including sideload) if a request for a single module is fired I had tried 我发现的所有内容仅体现了如果我尝试过对单个模块的请求被触发,则回复应为什么样(包括侧面负荷)

{
   "custom_routes": [
      {
         "custom_route": {
            "id": 1,
            "name": "Route 1",
            "description": "Description 1",
            "station_ids": [
               1,
               2,
               3
            ]
         }
      }
   ],
   "stations": [
      {
         "id": 1,
         "name": "Station 1",
         "description": "Description 1",
      }
   ]
}

but that didn't work. 但这没用。 But if the keyname "custom_route" for a single represtentation of the model is left of the "custom_routes" entries are displayed. 但是,如果只剩下一个表示模型的键名“ custom_route”,则会显示“ custom_routes”条目。 Hopefully there is a work around for that problem and hopefully my english was good enough to describe the issue. 希望能解决这个问题,并希望我的英语足以描述这个问题。 I am neither an native speaker nor a long-time web developer. 我既不是母语人士,也不是长期的Web开发人员。 But looking forward to improve both :) 但是期待改善两者:)

Ember Data made a breaking change between RC 13 and 1.0, see the transition Ember Data在RC 13和1.0之间进行了重大更改,请参见过渡

https://github.com/emberjs/data/blob/master/TRANSITION.md https://github.com/emberjs/data/blob/master/TRANSITION.md

for additional breaking changes. 进行其他重大更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM