我目前在预选 View 中预选项目时遇到问题。我正在将ember 1.3与ember-data 1.0.0 beta 6和ember-data-django-rest-adapter一起使用。
App.Article = DS.Model.extend({
title: attr(),
description: attr(),
authors: hasMany('author')
});
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('article', params.article_id);
},
setupController: function(controller, model) {
controller.set('content', model);
}
});
App.ArticleController = Ember.ObjectController.extend({
needs: ['authors'],
allAuthors: function() {
return this.store.find('author');
}.property()
});
模板:
{{input authors as='select'
multiple='true'
collection='allAuthors'
selection='authors'
optionValuePath='content.id'
optionLabelPath='content.name'}}
我不确定为什么这不起作用,因为当我在模板中使用#each输出 allAuthors 和 authors 时,我正在获取应该的数据。
有什么我想念的吗?
先谢谢您的帮助。
最佳答案
我通常使用 promise 在路由中预先填充此类数据:
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('article', params.article_id);
},
setupController: function(controller, model) {
this.store.find('author').then(function(authors) {
controller.set('allAuthors', authors);
// or maybe controller.get('allAuthors').addObjects(authors);
});
controller.set('content', model);
}
});
App.ArticleController = Ember.ObjectController.extend({
needs: ['authors'],
allAuthors: []
});
不知道这是否是执行此操作的最佳方法,但这对我有用。