如何通过控制器动作编程添加组件 [英] How to programatically add component via controller action
问题描述
如果我手动将其添加到模板中,组件将很好地运行,如:
{{#each}}
< div> blah blah {{my-component data = this.something action =doSomething}}< button {{action'add'this}}>添加< / button>< / div>
{{/ each}}
我可以使用标志显示/隐藏组件,并在我们点击添加按钮时切换它,但是如果可能的话我宁愿动态执行。
我没有尝试这个,但是对我来说没有效果,访问商店:
操作:{
add:function(obj){
var view = Ember.View.create({
template:Ember.Handlebars.compile('{{my-component action =addQuestion}}')
});
view.set('data',obj.get('something'));
Ember.run(function(){
// promly可以得到父视图而不是document.body
view.appendTo(document.body);
} );
}
}
谢谢。
我认为这个例子回答了你的问题:
http://emberjs.jsbin.com/axUNIJE/1/edit
I have a scenario where I have list of items and each item has a create button. When I click on create, I wanted a component to be appended to the list item. This component uses model data as parameter and also accesses store from within. To access the store in the component I am using targetObject.store
The component works well if I add it to the template manually like:
{{#each}}
<div> blah blah {{my-component data=this.something action="doSomething"}} <button {{action 'add' this}}>Add</button></div>
{{/each}}
I can probably show/hide the component using a flag, and toggle it when we click on Add button, but I rather do it dynamically if possible.
I did try this but didn't work for me because I couldn't access store :
actions: {
add: function(obj){
var view = Ember.View.create({
template: Ember.Handlebars.compile('{{my-component action="addQuestion"}}')
});
view.set('data', obj.get('something'));
Ember.run(function() {
//prolly can get parent view rather than document.body
view.appendTo(document.body);
});
}
}
Thanks.
I think this example answers your question:
http://emberjs.jsbin.com/axUNIJE/1/edit
这篇关于如何通过控制器动作编程添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!