【发布时间】:2015-09-02 21:08:47
【问题描述】:
我正在尝试在 Ember 2.0.1 中实现嵌套组件,但在操作处理程序中使用 toggleProperty 函数时出现了奇怪的行为。
第一个组件看起来像:
// ./components/comp-1.js
import Ember from 'ember';
export default Ember.Component.extend({
prop1: false,
hello: "Default text of comp-1",
_changeHello: function() {
this.set('hello', 'Text set by comp-1');
}.on("init"),
actions: {
customAction1() {
this.toggleProperty('prop1');
}
}
});
.
// ./templates/components/comp-1.hbs
<button {{action 'customAction1'}}>{{hello}}</button>
第二个是:
// ./components/comp-2.js
import Ember from 'ember';
export default Ember.Component.extend({
data: [],
_doSomeImportentStuff: function() {
var data = this.get('data');
data = [{name: 'Text set by comp-2', bool: false},
{name: 'Text set by comp-2', bool: true}];
this.set('data', data);
}.on("init")
});
.
// ./templates/components/comp-2.hbs
{{#each data as |d|}}
{{comp-1 hello=d.name prop1=d.bool}}
{{/each}}
组件comp-2 创建了两个名称为文本由comp-1设置的按钮。如果我单击一个按钮,则文本更改为 由 comp-2 设置的文本,因为执行了在动作处理程序 customAction1 中调用的函数 this.toggleProperty('prop1') .如果我删除此功能或从 ./templates/components/comp-2.hbs 中删除 prop1 的设置,那么一切都会按预期工作,即按钮的文本始终保持为 由 comp-1设置的文本>.
为什么toggleProperty 函数会设置其他属性?
我做错了吗?
可以在此处查看实际行为:http://ember-twiddle.com/90798b4952deb4a83de1
【问题讨论】:
-
这方面有什么成功吗?
标签: javascript ember.js ember-components