How to bind value form input select to attribute in controller

后端 未结 4 828
执笔经年
执笔经年 2020-12-16 05:10

I try to bind value from input select to attribute \"selectedValue\" in controller.

This is app.js

Food = Ember.Application.create();

Food.appsContr         


        
4条回答
  •  别那么骄傲
    2020-12-16 05:25

    Using a custom Ember.View works for me, but I think there is a better solution...

    See working example is this fiddle http://jsfiddle.net/pangratz/hcxrJ/

    Handlebars:

    {{#view Food.SelectView contentBinding="Food.foodController"
        valueBinding="Food.appsController.selectedValue"}}
        
    {{/view}}
    

    app.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"}));
    

提交回复
热议问题