1

我想知道如何在路由器中创建一个模型,其属性取自控制器的“需要”功能给出的依赖项。我正在使用 ember-1.0.0-rc.3。

这是我的应用程序现在的样子,稍微简化了一点:

App.LeagueAddMatchRoute = Ember.Route.extend({
    model: function (params) {
        var match = App.Match.createRecord({});
        return match;
    },
    setupController: function (controller) {
        controller.set('players', App.Player.find());
    }

});

App.LeagueAddMatchController = Ember.ObjectController.extend({
    needs: "league",
    save: function () {
        this.get('model').get('transaction').commit();
        return this.transitionToRoute('leagues');
    }
});

App.Router.map(function () {
    this.resource('leagues', function () {
        this.resource('league', { path: ':league_id' }, function () {
            this.route('addMatch');
        })
    });
});

App.LeaguesRoute = Ember.Route.extend({
    model: function () {
        return App.League.find();
    }
});

App.League = DS.Model.extend({
    name: DS.attr('string'),
    matches: DS.hasMany('App.Match')
});

App.Match = DS.Model.extend({
    league: DS.belongsTo('App.League'),
    p1: DS.belongsTo('App.Player', {inverse: 'p1matches'}),
    p2: DS.belongsTo('App.Player', {inverse: 'p2matches'}),
    p1Score: DS.attr('number'),
    p2Score: DS.attr('number')
});

这就是我想要完成的:

App.LeagueAddMatchRoute = Ember.Route.extend({
    model: function (params) {
        var match = App.Match.createRecord({league: controller.league });
        return match;
    },
    setupController: function (controller) {
        controller.set('players', App.Player.find());
    }

});
4

1 回答 1

1

从路由内部,您可以使用以下命令访问不同的控制器:

this.controllerFor("theControllerIWant");

所以你可以这样做:

App.LeagueAddMatchRoute = Ember.Route.extend({
  model: function (params) {
    var match = App.Match.createRecord();
    match.set('league', this.controllerFor("league"));
    return match;
  },
  setupController: function (controller) {
    controller.set('players', App.Player.find());
  }
});

希望能帮助到你

于 2013-05-25T13:42:09.253 回答