2

単一のプロファイルを表す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);            
     }
});
4

2 に答える 2

4

を手動で作成する必要がないようitemControllerに、ループ内でプロパティを設定できます。eachProfileController

{{#each profile in profiles itemController="profile"}}
    {{view App.ProfileView profileBinding=profile}}
{{/each}}

次に、 への参照が each に挿入されてneedsいることを確認するために使用できます。それ自体にアクセスするために使用します。The Ember Way を実行している場合は、そのコントローラー ( ) のプロパティにアクセスする必要があります。The Ember Way ではなく、プロパティではなくそのコントローラーのプロパティにコレクションを持っているようです。その場合は、 を使用します。の最初のインスタンスは を取得し、2 番目のインスタンスはそのコントローラに設定されているプロパティを取得します。ProfilesControllerProfileControllerthis.get('controllers.profiles')ArrayControllercontentthis.get('controllers.profiles.content')profilescontentthis.get('controllers.profiles.profiles')profilesProfilesControllerprofiles

App.ProfileController = Ember.ObjectController.extend({
    needs: ['profiles'],
    actions: {
        edit: function() {

            this.get('controllers.profiles.profiles').forEach(function(profile) {
                profile.set('isActive', false);
            });

            // enable current profile
            this.set('isActive', true);
        }
    }
});
于 2013-10-08T21:14:47.623 に答える
1

暗黙的にしたいですか (他の言語で super を使用するように)、それとも明示的にアクセスしたいコントローラーに名前を付けるだけで十分ですか?

このリンクを参照してください: http://eviltrout.com/2013/02/04/ember-pre-1-upgrade-notes.html

あなたの場合は次のようになります。

App.ProfileController = Ember.ObjectController.extend({
    needs: ['profiles'],
    actions: {
        edit: function() {

            this.get('controllers.profiles').forEach(function(profile) {
                profile.set('isActive', false);
            });

            // enable current profile
            this.set('isActive', true);
        }
    }
});
于 2013-10-08T16:47:09.487 に答える