编辑:我想我找到了解决方案。正如我在问题中所说,该变量profiles
是一个承诺,所以我尝试了以下方法并且它有效:
...
setupController: function(controller, model) {
controller.set('model', model);
var profiles = App.Profile.findAllByMaster(model.get('id'));
profiles.then(function(data) {
controller.set('profiles', data);
});
}
...
结束编辑
我遇到了错误:Assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed [object Object]
当我尝试从setupController
钩子中的另一个模型获取数据时。
路线是MastersMaster
关联的模型Master
,我尝试获取Profiles
属于当前的模型Master
。
我没有使用 Ember Data 或类似的东西。它只是带有 $.ajax 调用的纯 jQuery。
很难解释,所以这里是代码摘录:
App.MastersMasterRoute = Ember.Route.extend({
model: function(params) {
return App.Master.find(params.master_id);
},
setupController: function(controller, model) {
controller.set('model', model);
// if I comment these two lines it works but I don't get the profiles (obviously)
var profiles = App.Profile.findAllByMaster(model.get('id'));
controller.set('profiles', profiles);
}
});
App.Profile = Ember.Object.extend({
id: null,
name: '',
master_id: null
});
App.Profile.reopenClass({
findAllByMaster: function(master_id) {
var profiles = Ember.A();
return $.ajax({
url: 'ajax/get.profiles.php',
type: 'GET',
dataType: 'json',
data: { master_id: master_id }
}).then(function(response) {
$.each(response, function(i, item) {
profiles.pushObject(App.Profile.create(item));
});
return profiles;
});
}
});
如果我在做之前console.log
的变量我看到它是一个承诺而不是预期的对象数组。我想我必须先解决承诺,但我不知道。profiles
controller.set
Profile
PS:对不起我的英语:(