【发布时间】:2015-06-26 12:36:43
【问题描述】:
当组件被动态添加到页面时,Ember (1.11.0) 组件上的 {{actions}} 出现问题。奇怪的是,它似乎与 ember 应用程序如何添加到页面有关 - 通过默认模板与添加到“rootElement”。
工作 JSBin:actions are triggered
非工作 JSBin:actions aren't triggered
我的组件定义:
<script type="text/x-handlebars" data-template-name="components/foo-bar">
<p>Component {{name}}</p>
<button {{action "doIt"}}>Do It</button>
</script>
App.FooBarComponent = Ember.Component.extend({
click: function() {
console.log('click fired! - ' + this.get('name'));
},
actions: {
doIt: function() {
console.log('doIt fired! - ' + this.get('name'));
}
}
});
当组件被动态添加到页面时,不会触发 click() 事件和 doIt() 操作。我正在使用 append() 方法将组件添加到页面:
App.IndexController = Ember.Controller.extend({
count : 1,
actions: {
createNewFoobBar: function() {
this.set('count', this.get('count') + 1);
var comp = this.container.lookup('component:foo-bar');
comp.set('name', this.get('count'));
comp.set('controller', this);
comp.append();
}
}
});
在任何一种情况下,当组件是模板的一部分时,动作都会被正确触发:
{{foo-bar name="one"}}
【问题讨论】:
-
我可以通过将 append() 调用更改为 appendTo('#content') 来让 click() 事件在我之前的 JSBin 示例中的组件上触发,但是组件的操作仍然没有t 按预期工作。 jsbin
标签: javascript ember.js