如何通过控制器动作编程添加组件 [英] How to programatically add component via controller action

查看:71
本文介绍了如何通过控制器动作编程添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我有一个项目列表,每个项目都有一个创建按钮。当我点击创建时,我想要一个组件被附加到列表项。该组件使用模型数据作为参数,并从内部访问存储。要访问组件中的商店,我正在使用targetObject.store



如果我手动将其添加到模板中,组件将很好地运行,如:

  {{#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆