[英]Ember 3.5 BelongsTo Relationships not Resolving
我曾經有我的emberjs 1.13,我將其升級到3.5,但在我無法訪問其中的那些數據時,belongsTo關系出現問題。 我的代碼如下
模型
export default DS.Model.extend( {
title: DS.attr( 'string' ),
description: DS.attr( 'string' ),
published: DS.attr( 'boolean' ),
publishedAt: DS.attr( 'date' ),
course: DS.belongsTo( 'course' ),
author: DS.belongsTo( 'profile', { async: true } ),
viewed: false,
isNew: true,
}
在控制器中
this.get('model.published') working
this.get('model.author.name') not working
但是相同的代碼正在處理emberjs 1.13
余燼數據1.13
余燼數據3.5
這是從1.13到3.5的巨大提升。
發生了很大變化。 為了完全了解更改的內容和原因,我強烈建議您每次升級次要或主要版本時都閱讀每個Ember發行說明 。 超級有幫助。
author
很可能沒有加載到商店中。 檢查數據是否已加載,檢查路線的model()
,網絡請求和Ember Inspector。
如果已加載,則可能是async: true
。 嘗試刪除它?
這是Ember 3.5中的一個工作示例:
app/models/thing.js
:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';
export default Model.extend({
// Attributes
title: attr('string'),
description: attr('string'),
published: attr('string'),
publishedAt: attr('string'),
// Relationships:
// No need for async: true
course: belongsTo('course'),
author: belongsTo('author')
});
app/models/author.js
:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
name: attr('string')
});
app/route/thing.js
import Route from '@ember/routing/route';
export default Route.extend({
model(params) {
// Assuming you use JSON API
// Make sure `author` is included when fetching `thing`
return this.store.query('thing', {
include: 'author, course'
}),
}
});
app/controllers/thing.js
import Controller from '@ember/controller';
export default Controller.extend({
init(){
console.log(this.model.author.name)
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.