从组件内调用ember组件操作

jml*_*mls 10 ember.js

我正在创建一个组件来包装select2选择框.代码如下:

App.FixedSelectComponent = Ember.Component.extend({
  actions: {
    change: function(value) {
      this.set('selectedValue',value);
    }
  },

didInsertElement : function(){
  this.$("#select1").select2().on("change", function(e) {
      if ($.isArray(e.val)) {
          $.each(e.val, function(index,value) {
              console.log("multiple:",value.split('>')[2].split('<')[0]);
             // send to change
          });             
      } else {
          console.log("single:",e.val.split('>')[2].split('<')[0]);  
          // send to change
      }
    });
  },

  willDestroyElement : function() {
    this.$("#select1").select2('destroy');
},

});
Run Code Online (Sandbox Code Playgroud)

但是,我所坚持的是如何将我在on("change")事件中获得的数据发送到action:更改我已经定义的,或者我是否可以在on中设置selectedValue属性("改变")事件

"this"不是"// send to change"行的组件 - 此时我如何/在何处获得对组件本身的引用?

基本上我想要实现的是将传递给select2的"change"事件的数据放入我的selectedValue属性中

谢谢

Lac*_*cek 9

你可以用Component.send('actionName').

我在Ember的文档中找到了它.


cho*_*per -1

尝试这个:

App.FixedSelectComponent = Ember.Component.extend({
  change: function(value) {
    this.set('selectedValue',value);
  }

  didInsertElement : function(){
    var self = this;
    this.$("#select1").select2().on("change", function(e) {
      if ($.isArray(e.val)) {
         $.each(e.val, function(index,value) {
             console.log("multiple:",value.split('>')[2].split('<')[0]);
             // send to change
             self.change(value); // substitute value by whatever you want to pass
         });             
      } else {
         console.log("single:",e.val.split('>')[2].split('<')[0]);  
         // send to change
         self.change(value); // substitute value by whatever you want to pass
      }
    });
  },

  willDestroyElement : function() {
    this.$("#select1").select2('destroy');
  },
});
Run Code Online (Sandbox Code Playgroud)