一つの方法は、単にコントローラに第二のリストを維持することである。
だけコントローラに別のリストを維持し、押圧
App.FooController = Ember.ArrayController.create({
selectedContent: [],
selectToggle: function(event) {
var selectedContent;
selectedContent = this.get(selectedContent);
if (selectedContent.contains(event.context)) {
return this.set('selectedContent', selectedContent.without(event.context));
} else {
return this.get('selectedContent').push(event.context);
}
}
});
<script type="text/x-handlebars" data-template-name="index">
<ul>
{{#each foo in controller}}
<li {{action selectToggle foo}}>{{foo.name}}</li>
{{/each}}
</ul>
</script>
は/アイテムが選択されたか否かに基づいて除去します。
また、Ember.ObjectProxyを使用して、「isSelected」プロパティを使用してfooオブジェクトの値を増やすこともできます。
App.FooController = Ember.ArrayController.create({
selectedContent: @get('content').map(function(item){
Ember.ObjectProxy.create({
content: item
isSelected: false
});
});
});
<script type="text/x-handlebars" data-template-name="index">
<ul>
{{#each foo in controller.selectedContent}}
<li {{bindAttr class= "foo.isSelected"}}{{action selectToggle foo}}>{{foo.name}}</li>
{{/each}}
</ul>
</script>
次に、selectToggleメソッドでは、fooのisSelectedプロパティを適切に設定するだけです。
ありがとう、アンドレ。私は今日それを試みます。 Ember.ArrayProxyのソリューションがどのように見えるか教えてください。私はEmberのすべての可能性を知り、これと多分将来の問題に適したソリューションを選択できるようにしたいと考えています。 – Marc
テンプレートからselectedContentリストにアクセスする方法を教えてください。例えば、選択した行を別のスタイルでレンダリングするなどですか? – Marc
ObjectProxyメソッドを使用する必要がありますか。私は答えを更新しました。 –