简体   繁体   English

如何使用Ember.js中的链接描述hasMany关系?

[英]How to describe hasMany relation using links in Ember.js?

I created model Consultation: 我创建了模型咨询:

import DS from 'ember-data';

export default DS.Model.extend({
    title: DS.attr('string'),
    records: DS.hasMany('record', { async: true }),
    currentUser: DS.belongsTo('user'),
    remoteUser: DS.belongsTo('user')
});

And also I created model Record: 我还创建了模型记录:

import DS from 'ember-data';

export default DS.Model.extend({
    record_text: DS.attr('string'),
    record_poster_id: DS.attr('string'),
    record_consultation_id : DS.attr('number'),
    consultation: DS.belongsTo('consultation'),
    isMine: DS.attr('boolean')
});

At first all consultations load during opening page. 首先,在打开页面期间加载所有咨询。 And I don't want to load all records of each consultation during opening page. 而且我不想在打开页面期间加载每次咨询的所有记录。 To do this I added async: true but all records loaded simultaneously sending many requests like /consultations/:id/records. 为此,我添加了async:true,但是同时加载了所有记录,并发送了许多请求,例如/ consultations /:id / records。 After that consultation and records still non-joined. 经过那次咨询,记录仍然没有加入。 I have next json response for consultation: 我需要咨询下一个json响应:

{  
   "consultations":[  
      {  
         "id":140721,
         "title":"ExpertId: 41217, clientId: 0",
         "links":{  
            "records":"records"
         },
         "currentUser":41217,
         "remoteUser":159984
      },
   ......
   ]
}

And for records: 并作记录:

{  
   "records":[  
      {  
         "record_id":681952,
         "record_consultation_id":140721,
         "record_poster_id":0,
         "record_text":"1",
      },
  ........
   ]
}

I think I need to override default serializer. 我想我需要重写默认的序列化程序。 I tried to create serializer for Record: 我试图为Record创建序列化器:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
    primaryKey: 'record_id',
    keyForRelationship: function(key, kind) {
        return 'record_consultation_id';
    }
});

But it still doesn't work. 但这仍然行不通。 Please advise how to join models using links? 请告知如何使用链接加入模型?

UPDATE: Template 更新:模板

{{#each item in model.records}}
<div class="table message">
            {{item.record_text}}
</div>
{{/each}}

I am doing Async (lazy loading) of data using RESTAdaptor and Ember-Data too. 我也在使用RESTAdaptor和Ember-Data进行数据的异步(延迟加载)。

For my links area, i put in the full request URL as follows: 对于我的链接区域,我输入了完整的请求URL,如下所示:

"links":{  
            "records":"/consultations/140721/records"
 },

And using firebug to look at when the request gets sent off, its only when I request for the async content that the AJAX gets fired off. 并且使用firebug来查看何时发出请求,只有当我请求异步内容时才发出AJAX。

model.get('records');

Can you provide your Controllers & Template code so I can see how your accessing the Records? 您能否提供您的控制器和模板代码,以便查看您如何访问记录?

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

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