如何将值表单输入选择绑定到控制器中的属性

问题描述:

我尝试将输入select中的值绑定到控制器中的属性“selectedValue”。如何将值表单输入选择绑定到控制器中的属性

这是app.js

Food = Ember.Application.create(); 

Food.appsController = Ember.Object.create({ 
    selectedValue: "" 
}); 

Food.Todo = Ember.Object.extend({ 
    title: null, 
    value: null 
}); 

Food.FoodController = Ember.ArrayProxy.create({ 
    content: [] 
}); 

Food.FoodController.pushObject(Food.Todo.create({title:"a", value:"1"})); 
Food.FoodController.pushObject(Food.Todo.create({title:"b", value:"2"})); 
Food.FoodController.pushObject(Food.Todo.create({title:"c", value:"3"})); 

这是index.html的

{{#collection 
    contentBinding="Todos.todosController" 
    tagName="select" 
    itemClassBinding="content.isDone"}} 
    {{content.title}} 
{{/collection}} 

输出这个样子的

<select id="ember180" class="ember-view"> 
    <option id="ember192" class="ember-view"> 
    <script id="metamorph-0-start" type="text/x-placeholder"></script> 
    a 
    <script id="metamorph-0-end" type="text/x-placeholder"></script> 
    </option> 
    <option id="ember196" class="ember-view"> 
    <script id="metamorph-1-start" type="text/x-placeholder"></script> 
    b 
    <script id="metamorph-1-end" type="text/x-placeholder"></script> 
    </option> 
    <option id="ember200" class="ember-view"> 
    <script id="metamorph-2-start" type="text/x-placeholder"></script> 
    c 
    <script id="metamorph-2-end" type="text/x-placeholder"></script> 
    </option> 
</select> 

我不知道如何增加价值选项如何将选定的值绑定回控制器。 这是可能在Emberjs做?

Ember现在有一个内置的Select视图。

你可以在最新的Ember中找到它。JS在这里建:http://cloud.github.com/downloads/emberjs/ember.js/ember-latest.js

下面是一个例子用法:

var 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.selectedPersonController = Ember.Object.create({ 
    person: null 
}); 

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'}) 
    ] 
}); 

您的模板看起来像:

{{view Ember.Select 
     contentBinding="App.peopleController" 
     selectionBinding="App.selectedPersonController.person" 
     optionLabelPath="content.fullName" 
     optionValuePath="content.id"}} 

同样,这里是一个的jsfiddle例如:http://jsfiddle.net/ebryn/zgLCr/

使用自定义Ember.View对我的作品,但我认为这是一个更好的解决办法...

见工作的例子是,小提琴http://jsfiddle.net/pangratz/hcxrJ/

把手:

{{#view Food.SelectView contentBinding="Food.foodController" 
    valueBinding="Food.appsController.selectedValue"}} 
    <select> 
     {{#each content}} 
      <option {{bindAttr value="value"}} >{{title}}</option> 
     {{/each}} 
    </select> 
{{/view}} 

应用.js:

Food = Ember.Application.create(); 

Food.SelectView = Ember.View.extend({ 
    value: null, 

    valueChanged: function(){ 
     this.$('select').val(this.get('value')); 
    }.observes('value'), 

    didInsertElement: function(){ 
     var self = this; 
     this.$('select').change(function(){ 
      var val = $('select option:selected').val(); 
      self.set('value', val); 
     }); 
    } 
}); 

Food.appsController = Ember.Object.create({ 
    selectedValue: "" 
}); 

Food.Todo = Ember.Object.extend({ 
    title: null, 
    value: null 
}); 

Food.foodController = Ember.ArrayProxy.create({ 
    content: [] 
}); 

Food.foodController.pushObject(Food.Todo.create({title:"a", value:"1"})); 
Food.foodController.pushObject(Food.Todo.create({title:"b", value:"2"})); 
Food.foodController.pushObject(Food.Todo.create({title:"c", value:"3"})); 
+0

该解决方案非常适用于我出于多种原因: a)Ember.Select视图将内容数组中的整个对象作为选定值返回,而我o很少有兴趣回到实际的选择价值。 b)我在这个实现上工作的速度要快得多:我不需要在标签和值上绑定,他们不会改变(我删除了上面的视图类扩展中的valueChanged方法,因为我没有需要它) 最后的备注:选择更改处理程序方法包含一个小错误(当您有多个选择器时):$('select options ... should be self。$('select options ... – Visionscaper 2012-05-10 21:50:49

从@pangrantz的解决方案中跳出来,这个Fidd例如(http://jsfiddle.net/bsyjr/)说明了一些改进:通过使用tagName,Handlebars代码更清晰。当tagName设置为“select”时,子视图自动成为“选项”元素。了解原因,请参阅https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/views/collection_view.js中的Ember.CollectionView.CONTAINER_MAP。在Javascript方面,通过指定itemViewClass,我们可以将value属性添加到选项元素。

<script type="text/x-handlebars" > 
    {{#collection Food.SelectView tagName="select" contentBinding="Food.foodController" 
     valueBinding="Food.appsController.selectedValue"}} 
     {{content.title}} 
    {{/collection}} 

    selected: {{view Ember.TextField valueBinding="Food.appsController.selectedValue"}}{{Food.appsController.selectedValue}} 
</script> 

Food = Ember.Application.create(); 

Food.SelectView = Ember.CollectionView.extend({ 
    value: null, 
    itemViewClass: SC.View.extend({ 
     attributeBindings:['value'], 
     valueBinding: 'content.value' 
    }), 

    valueChanged: function(){ 
     this.$().val(this.get('value')); 
    }.observes('value'), 

    didInsertElement: function(){ 
     var self = this; 
     this.$().change(function(){ 
      var val = $('select option:selected').val(); 
      self.set('value', val); 
     }); 
    } 
}); 

Food.appsController = Ember.Object.create({ 
    selectedValue: "" 
}); 

Food.Todo = Ember.Object.extend({ 
    title: null, 
    value: null 
}); 

Food.foodController = Ember.ArrayProxy.create({ 
    content: [] 
}); 

Food.foodController.pushObject(Food.Todo.create({title:"a", value:"1"})); 
Food.foodController.pushObject(Food.Todo.create({title:"b", value:"2"})); 
Food.foodController.pushObject(Food.Todo.create({title:"c", value:"3"})); 

仍有余地,事件处理,这是不使用Ember的事件框架的改进,这将使很多意义使用自定义的书面选择查看,不利用把手,因为IMO,在这种情况下,Handlebars增加了多少价值是可疑的。

+3

值得注意的是{@#}}`助手已被弃用,因为@SPangratz使用`{{#each}}'可以更清晰地实现相同的结果。 – 2011-12-16 15:51:19

+0

为了进一步澄清,还有一些地方'{{ {#collection}}`是有用的,但是我会先尝试使用`{{#each}}`,如果你不能得到你想要的结果,那就考虑`{{#collection}}`我们计划进一步改进`{{#each}}`,这将涵盖更多的c ASES。 – 2012-04-02 02:34:54

我不确定这是否对其他人有用,但我一直在根据这里的答案做类似的事情,并且让这个SelectView也能在这种情况下工作。它绑定到'更改',制定当前选中的视图,然后对其内容执行某些操作。

Food.SelectView = Ember.CollectionView.extend({ 
    change: function(e) { 
    var selected = this.$().find(":selected").index(); 
    var content = this.get('childViews')[selected].content; 
    // Do something with the result 
    Food.appsController.set('selectedValue', content.title); 
    } 
}); 

这样你就可以传递一个对象而不是select的索引。