単一のプロファイルを表すProfilesController
と があります。ProfileController
// Profiles listing
App.ProfilesController = Ember.ArrayController.extend({
});
// Single Profile representation
App.ProfileController = Ember.ObjectController.extend({
isActive: false,
actions: {
edit: function() {
// TODO: disable all profiles
// does not work ... parentController is undefined. pseudo code only!
this.get('parentController.children').forEach(function(profile) {
profile.set('isActive', false);
});
// enable current profile
this.set('isActive', true);
}
}
});
これは完全なコードではないことに注意してください。これらの 2 つのコントローラーには、私のアプリケーションにさらにコードがあり、プロファイルの一覧表示を可能にする ProfileView もあります。
重要な部分は、ProfileController 内から親の ProfilesController (ArrayController) にアクセスする方法です。
私は(編集アクションで)試しました:
this.get('parent') // => undefined
this.get('parentController') // => null
this.get('target') // => null
編集:その間、私はオブジェクトツリーを下って行き、信じられないほどハックだが実用的な解決策を思いついた:
// disable all profiles
this.get('controllers.profiles.target._activeViews.profiles.0._childViews.0._childViews').forEach(function(childView) {
childView.get('_childViews.0.controller').set('isActive', false);
});
テンプレート構造で何かを変更するまで機能します。これを行うには、よりクリーンな方法が必要です:-D
編集2:もう少し明確にするために、ここに私のプロファイルテンプレートがあります。ここには、プロファイルprofiles
モデルのコレクションがあります(コントローラーではありません!現在のアクティブなプロファイルなどのアプリケーションの状態を保存する必要があるため、すべてのモデルはコントローラーによって表されます):
{{#each profile in profiles}}
{{view App.ProfileView profileBinding=profile}}
{{/each}}
および ProfileView
App.ProfileView = Ember.View.extend({
templateName: 'profiles/profile',
init: function() {
this._super();
var profile = this.get('profile');
// Here I explicitely create a ProfileController instance,
// otherwise ProfilesController would be used instead of ProfileController
// because in EmberJS by default the View's controller is always the parent controller
this.set('controller', App.ProfileController.create());
var controller = this.get('controller');
controller.set('profile', profile);
}
});