我有一个场景需要将数据的值(通常在运行时检索)绑定到表单元素。在这种情况下,元素是一个 Ember.Select,它将针对任意长度的数据集重复呈现。
jsFiddle可以在这里找到简单的例子
<script type="text/x-handlebars">
{{#each App.simpleSelectionArray.content}}
{{this.firstName}}
{{view Ember.Select
contentBinding="App.peopleController.content"
selectionBinding="this"
optionLabelPath="content.fullName"
optionValuePath="content.id"}}
<p>Selected: {{this.fullName}}
(ID: {{this.id}})</p>
{{/each}}
{{#each App.selectionArray.content}}
{{this.person.firstName}}
{{view Ember.Select
contentBinding="App.peopleController.content"
selectionBinding="this.person"
optionLabelPath="content.fullName"
optionValuePath="content.id"}}
<p>Selected: {{this.person.fullName}}
(ID: {{this.person.id}})</p>
{{/each}}
</script>
</p>
window.App = Ember.Application.create();
App.Person = Ember.Object.extend({
id: null,
firstName: null,
lastName: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName').cacheable()
});
App.peopleController = Ember.ArrayController.create({
content: [
App.Person.create({id: 1,firstName: 'Yehuda',lastName: 'Katz'}),
App.Person.create({id: 2,firstName: 'Tom',lastName: 'Dale'}),
App.Person.create({id: 3,firstName: 'Peter',lastName: 'Wagenet'}),
App.Person.create({id: 4,firstName: 'Erik',lastName: 'Bryn'})
]
});
App.simpleSelectionArray = Ember.ArrayController.create({
content: [
App.peopleController.objectAt(1)
]});
App.selectionArray = Ember.ArrayController.create({
content: [
{
id: '2',
person: Ember.computed(function(key, value) {
if (arguments.length === 1){
var personId = this.get('id');
console.log(personId);
var listedPerson = App.peopleController.content.findProperty("id", personId);
return listedPerson ;
}else{
this.set(key,value);
return value;
}
}).property('id').cacheable()
},
]});
这两个生成的选择元素基于不同的数据结构。第一个(大部分)表现如预期,除了绑定后的选择。
第二种数据结构更典型,对我来说根本不起作用。
有什么想法吗?