10

我正在创建一个组件来包装 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');
},

});

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

“this”不是“// send to change”行中的组件-此时我如何/在哪里获取对组件本身的引用?

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

谢谢

4

5 回答 5

10

您可以使用Component.send('actionName').

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

于 2014-09-11T08:56:06.850 回答
2

thiscontext 不会引用 FixedSelectComponent 中的上下文$.each,并且还会使用 send 方法,该方法将调用 FixedSelectComponent 更改方法..

参考:http ://emberjs.com/api/classes/Ember.Component.html#method_send

didInsertElement : function(){
  var _this = 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]);
             _this.send('change',value.split('>')[2].split('<')[0]); // send to change
          });             
      } else {
          console.log("single:",e.val.split('>')[2].split('<')[0]);  
          _this.send('change',e.val.split('>')[2].split('<')[0]); // send to change
      }
    });
  }
于 2016-08-03T07:41:38.907 回答
0
this.get('actions').change.call(this, value);

检查http://emberjs.com/api/classes/Ember.Component.html#property_actions -'actions'只是组件上的另一个属性。

于 2016-08-03T05:26:52.220 回答
-1
this._actions['change'].apply(this, value);
于 2013-10-24T00:45:02.443 回答
-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');
  },
});
于 2013-09-21T17:42:20.970 回答