私はEmberJSを初めて使用します。単純なhasManyモデル化をレンダリングしようとしています。
私のアプリは、複数の人の複数のタスクを処理することになっています。
まず、モデルがあります。
App.Task = Em.Object.extend({
name: null
});
App.Person = Em.Object.extend({
firstname: null,
lastname: null,
avatar: null,
tasks:null,
});
人は複数のタスクを持つことができます。したがって、私のpersonsControllerは次のように機能します。
App.personsController = Em.ArrayController.create({
content: [],
tasks:[],
addPerson: function(){
var aPerson = App.Person.create({
firstname: this.firstname,
lastname : this.lastname,
avatar : this.avatar,
});
this.pushObject( aPerson );
}
私のtasksControllerは、タスクジョブを処理します。
App.tasksController = Em.ArrayController.create({
content:[],
contentBinding: "App.personsController.tasks",
name:'',
removeTask: function(e){
this.removeObject( e.context );
},
addTask: function(e){
this.pushObject( App.Task.create({"name":this.name}) );
}
});
contentBiding
ここでの鍵の1つであるようです。作業中の人のタスクに自動バインドする必要があります。
ビュー側では、ハンドルバーテンプレートで作業しています。
<script type="text/x-handlebars">
{{view Em.TextField valueBinding="App.personsController.firstname" placeholder="firstname" }}
{{view Em.TextField valueBinding="App.personsController.lastname" placeholder="lastname" }}
<button {{action "addPerson" target="App.personsController"}} class="btn btn-primary" >Ajouter un developpeur</button>
{{#each App.personsController}}
<div>
<h3>{{firstname}}{{lastname}}</h3>
{{view Em.TextField valueBinding="App.tasksController.name"}}
<button {{action "addTask" target="App.tasksController"}} >Add a task</button>
{{#each App.tasksController}}
{{view Em.TextField valueBinding="name" }}
<button {{action "removeTask" target="App.tasksController"}} >x</button>
{{/each}}
</div>
{{/each}}
</script>
したがって、新しい人を追加するときは、すべてが正常に機能します。ただし、タスクを追加すると、ビュー内のユーザーの各タスクにタスクが追加されます。
概念的な間違いを犯しているのではないかと思います。この種の視覚化に関する適切なドキュメントを見つけることができません。hasManyとember.dataの関係の例を見てきましたが、最初にここで何が起こっているのかを正しく理解したいと思います。