1

编辑:我想我找到了解决方案。正如我在问题中所说,该变量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的变量我看到它是一个承诺而不是预期的对象数组。我想我必须先解决承诺,但我不知道。profilescontroller.setProfile

PS:对不起我的英语:(

4

1 回答 1

1

正如我在编辑中所说,问题在于该findAllByMaster方法返回一个承诺,因此必须先解决它,然后再将其分配给控制器的属性。

我想有一种更优雅或更有效的解决方法,因此欢迎使用其他解决方案。

于 2013-08-03T22:49:26.807 回答