コントローラーの「ニーズ」機能によって提供される依存関係から取得したプロパティを使用して、ルーターでモデルを作成する方法を知りたいです。私は 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());
}
});