我正在升级,我遇到了一些问题,因为 ArrayController 已被弃用。
在我使用的旧 Ember 1.13 路线中
模型/公告.js
export default DS.Model.extend( {
id:DS.attr('string'),
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,
}
序列化器/announcement.js
import DS from 'ember-data';
import ApplicationSerializer from 'mim/serializers/application';
export default ApplicationSerializer.extend( DS.EmbeddedRecordsMixin, {
keyForRelationship: function( key ) {
return key !== 'author' ? key : 'id';
}
} );
路线/公告.js
setupController: function( controller, model ) {
this._super( ...arguments );
var announcements = Ember.ArrayController.create( {
model: model,
sortProperties: [ 'publishedAt' ],
sortAscending: false
} );
controller.set( 'model', announcements );
},
在路由公告的控制器中,如下:
控制器/announcement.js
publishedAnnouncements: Ember.computed( 'model.[]', 'model.@each.published', 'model.@each.viewed', function() {
var published = this.get( 'model' ).filterBy( 'published', true ),
announcements = Ember.A();
announcements.pushObjects( published.filterBy( 'viewed', false ) );
announcements.pushObjects( published.filterBy( 'viewed' ) );
return announcements;
} ),
所以在模板中我为每个循环运行一个来呈现所有公告,比如
模板/公告.hbs
{{#each publishedAnnouncements as |announcement|}}
{{announcement.author.firstName}}
{{/each}}
在 ember 升级 3.5 之后,我将这些更改为以下内容:
模型/公告.js
export default DS.Model.extend( {
id:DS.attr('string'),
title: DS.attr( 'string' ),
description: DS.attr( 'string' ),
published: DS.attr( 'boolean' ),
publishedAt: DS.attr( 'date' ),
course: DS.belongsTo( 'course' ),
// 从配置文件中删除 async true
author: DS.belongsTo( 'profile'),
viewed: false,
isNew: true,
}
序列化器/announcement.js
import DS from 'ember-data';
import ApplicationSerializer from 'mim/serializers/application';
export default ApplicationSerializer.extend( DS.EmbeddedRecordsMixin, {
keyForRelationship: function( key ) {
return key !== 'author' ? key : 'id';
}
} );
路线/公告.js
setupController: function( controller, model ) {
this._super( ...arguments );
//removed arrayController from here and assigned model
controller.set( 'model', model );
},
控制器/announcement.js
sortProperties: ['publishedAt:desc'], sortedModel: computed.sort('model', 'sortProperties'),
publishedAnnouncements: Ember.computed( 'model.[]', 'model.@each.published', 'model.@each.viewed', function() {
//getting model by local computed property defined above.arrayController sort is doing with above method by sortPropteries
var published =this.get('sortedModel').filterBy( 'published', true);
announcements = Ember.A();
announcements.pushObjects( published.filterBy( 'viewed', false ) );
announcements.pushObjects( published.filterBy( 'viewed' ) );
return announcements;
} ),
模板/公告.hbs
{{#each publishedAnnouncements as |announcement|}}
{{announcement.author.firstName}}
{{/each}}
然后announcement.author.firstname
在 ember 3.5 中未定义,但如果它不是 belongsTo 关系,它将存在(示例announcement.publishedAt
)
我不知道我错过了什么或我在这里做错了什么。
我在此处附上了我在控制器发布变量中所做的控制台日志的屏幕截图。
余烬 1.13
你的回答让我更好地理解了这个问题。api返回自定义版本的数据,这就是embeddedRecordsMixin使用的原因 这是课程的api有效负载
{
"courses": [{
"created_at": "2016-11-22T09:37:53+00:00",
"updated_at": "2016-11-22T09:37:53+00:00",
"students": ["01", "02"],
"coordinators": ["001", "002"],
"programme_id": 1,
"announcements": [{
"created_at": "2016-11-23T08:27:31+00:00",
"updated_at": "2016-11-23T08:27:31+00:00",
"course_id": 099,
"id": 33,
"title": "test announcement",
"description": "please ignore",
"published_at": "2016-11-23T08:27:31+00:00",
"published": true
}, {
"created_at": "2016-11-25T07:13:18+00:00",
"updated_at": "2016-11-25T07:13:18+00:00",
"course_id": 04,
"id": 22,
"title": "test before ",
"description": "test",
"published_at": "2016-11-25T07:13:18+00:00",
"published": true
}]
}