[英]call an ember component action from within the component
I am creating a component to wrap the select2 select box. 我正在创建一个组件来包装select2选择框。 The code is below:
代码如下:
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');
},
});
however, what I am stuck at is how to send the data that I've got in the on("change") event to the action:change that I've defined , or if I can set the selectedValue property itself in the on("change") event 但是,我所坚持的是如何将我在on(“change”)事件中获得的数据发送到action:更改我已定义的,或者我是否可以在on中设置selectedValue属性(“改变”)事件
"this" isn't the component at the "// send to change" lines - how / where do I get the reference to the component itself at this point ? “this”不是“// send to change”行的组件 - 此时我如何/在何处获得对组件本身的引用?
basically what I am trying to achieve is to get the data passed to the "change" event of select2 into my selectedValue property 基本上我想要实现的是将传递给select2的“change”事件的数据放入我的selectedValue属性中
thanks 谢谢
You can use Component.send('actionName')
. 您可以使用
Component.send('actionName')
。
I found it in Ember's documentation . 我在Ember的文档中找到了它。
this
context will not refer to FixedSelectComponent context in $.each
, and also use send method which will call FixedSelectComponent change method.. this
上下文不会引用$.each
FixedSelectComponent上下文,也会使用send方法调用FixedSelectComponent更改方法。
refer : http://emberjs.com/api/classes/Ember.Component.html#method_send 参考: 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
}
});
}
this.get('actions').change.call(this, value);
检查http://emberjs.com/api/classes/Ember.Component.html#property_actions - 'actions'
只是Component上的另一个属性。
Try this: 试试这个:
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');
},
});
this._actions['change'].apply(this, value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.