1

私たちは Ember アプリを構築しており、ネストされたビューを多数使用していますが、これまでのところ、1 つの問題を除いてすべてうまくいっています。ネストされたビューの 1 つにアクションを実装しようとすると、問題が発生します。

問題のビューは次のとおりです。

App.TodoView = Ember.View.extend({
  templateName: 'app/templates/todo' 
});

App.Todo_entriesView = Ember.View.extend({
  templateName: 'app/templates/todo_entries'
});

App.Todo_entryView = Ember.View.extend({
  templateName: 'app/templates/todo_entry',
});

テンプレートは次のとおりです。

/templates/todo.hbs

<article>
  <h1>{{title}}</h1>  
  {{outlet}}
</article>

/templates/todo_entries.hbs

{{#if isLoading}}
  <p>Loading...</p>
{{else}}
  <ul class="list">
    {{collection contentBinding="content" itemViewClass="App.Todo_entryView"}}
  </ul>
{{/if}}

/templates/todo_entry.hbs

<li>
{{#if isLoading}}
  Loading...
{{else}}
  {{view.content.id}}) {{view.content.title}}
  <a {{action deleteRecord href="true" target="controller"}}>Delete</a>
{{/if}}
</li>

コントローラーは次のとおりです。

App.TodoController = Ember.ObjectController.extend({
  deleteRecord: function() {
    this.get('content').deleteRecord();
    this.transaction.commit();
    App.router.transitionTo('todo');
  }
});

App.Todo_entriesController = Ember.ArrayController.extend();

App.Todo_entryController = Ember.ObjectController.extend({
  deleteRecord: function() {
    this.get('content').deleteRecord();
    this.transaction.commit();
    App.router.transitionTo('todo');
  }
});

削除ボタンをクリックすると、予想どおり、deleteRecord メソッドが Todo_entriesController (つまり、ビューの親) に存在せず、Todo_entryController にも存在しないというエラーが発生します。

Todo_entry テンプレートの deleteRecord アクションを取得して、Todo_entryController の deleteRecord メソッドを正しく呼び出す方法を知っている人はいますか? または、これを完全に行うためのより良い方法はありますか?

ありがとう、ダン

4

1 に答える 1

1

App.Todo_entryView は connectOutlet を使用せずに作成されるため、App.Todo_entryController に自動的にバインドされず、parentView コントローラーを使用します。

おそらく回避策は、App.Todo_entryView の init メソッドをオーバーライドし、コントローラーを手動でインスタンス化することです。何かのようなもの:

App.Todo_entryView = Ember.View.extend({
  templateName: 'app/templates/todo_entry',
  init: function(){
    this._super();
    this.set(
      'controller',
      App.Todo_entryController.create({ content: this.get('content') })
    );
  }
});
于 2012-10-23T16:28:33.683 に答える