配置模型钩子和setupController

时间:2013-04-29 06:07:13

标签: ember.js ember-data

我无法让Route的模型和setupController挂钩正常工作。对于我来说,我还是比较新的。这是我的代码

IS.AlbumsRoute = Ember.Route.extend({
    model: function (params) {
        return IS.Album.find(params.album_id);
    },
    setupController: function(controller, user) {
        var controller = this.controllerFor("application");
        var data = controller.get("currentUser").get('albums');
        controller.set('content', data);
    }
}

我的模特看起来像这样

IS.User = DS.Model.extend({
    email: DS.attr('string'),
    password: DS.attr('string'),
    albums: DS.hasMany('IS.Album'),
});

IS.Album = DS.Model.extend({
    name: DS.attr('string'),
    description: DS.attr('string'),
    user: DS.belongsTo('IS.User')
});

虽然这有效但是我的api的额外查询被发送到查询之前的“api / albums”,实际上获得了正确的数据(在url“api / albums / ids [] = 0& ids [] = 1” )。反正有没有阻止这个额外的查询形式发生?

1 个答案:

答案 0 :(得分:3)

你试过这个吗?

IS.AlbumsRoute = Ember.Route.extend({
    model: function () {
        var controller = this.controllerFor("application");
        return controller.get('currentUser.albums');
    }
}