使用ember-data中的ajax从父路由设置控制器内容

时间:2013-10-08 16:33:20

标签: ember.js ember-data

我有嵌套模型定义如下:

AS.ExerciseRun = DS.Model.extend({
    'name' : DS.attr('string'),
    'exercise' : DS.belongsTo('exercise')
});

AS.Exercise = DS.Model.extend({
    'name'          : DS.attr('string'),
    'engagement'    : DS.belongsTo('engagement')
});

AS.Engagement = DS.Model.extend({
    'name'          : DS.attr('string')
});

我需要在我的一个名为“AnalyticsRunsIndex”的数组控制器中使用“ExerciseRun”模型。我将它添加到arrayController的needs属性中。我现在有点困,因为我不确定如何通过AnalyticsRunsIndexRoute将数据加载到我的ExerciseRunController中。我的路线代码是:

AS.AnalyticsRunsIndexRoute = Ember.Route.extend({
    exerciseRunId: null,
    model: function () {
        .....

    },
    setupController: function (controller, model) {
        this._super(controller, model);
        controller.set('exerciseRunId', this.get('exerciseRunId'));
        this.controllerFor('analyticsTemplates').set('model', controller.get('store').find('analyticsTemplate'));


                    //TRIED AND FAILED!!!
                    //this returns a rejected promise  with error TypeError: payload[prop].map is not a function
        //this.controllerFor('exerciseRun').set('model', controller.get('store').find('exerciseRun', {'id': this.get('exerciseRunId')}));


        //controller.get('store').find('exerciseRun', {'id': this.get('exerciseRunId')}).then(function(data){
        //  this.controllerFor('exerciseRun').set('content',data);
        //});

        //controller.get('store').find('exerciseRun', {'id': this.get('exerciseRunId')}).then(function(data){
        //    controller.get('store').pushPayload('exerciseRun', data);
        //    controller.set('exerciseRun',data);
        //});


        //set exerciseRun details to the arrayController from any one record
        /*
        var store = controller.get('store');
        $.ajax({
            type: "GET",
            url: AS.baseURL + "exerciseRuns",
            data: {"id": this.get('exerciseRunId')},
            success: $.proxy(function (data) {
                //controller.set('exerciseRunData', data);
            }, this),
            dataType: "JSON"
        });
        */


    }
});

如何将此数据传输到适当的控制器?我试过简单地将属性设置为ArrayController的实例,但这也不起作用。任何帮助都感激不尽。感谢

1 个答案:

答案 0 :(得分:1)

我认为在你的setupController钩子里,你应该能够做到这样的事情:

// Get the controller outside of the promise.
// Inside the promise 'this' points to something different.
var exerciseRunController = this.controllerFor('exerciseRun');
controller.get('store').find('exerciseRun', {'id': this.get('exerciseRunId')}).then(function(data){
  exerciseRunController.set('content',data);
});