我有类似下面的代码在我的模板:兄弟itemControllers
{{#each items itemController="item"}}
有没有什么办法让引用同级itemController
S中itemController
S的一个实例内?
我有类似下面的代码在我的模板:兄弟itemControllers
{{#each items itemController="item"}}
有没有什么办法让引用同级itemController
S中itemController
S的一个实例内?
我在删除这个答案,因为回想起来,虽然它在技术上是正确的,但并不是正确的。
您需要将有关具有特定状态的孩子的信息存储在父控制器中。看一个例子
App.Child = Ember.ObjectController.extend({
isEdited: false,
actions: {
toggleEditingMode: function() {
old = this.get('parentController.currentlyEditing');
if(typeof old !== "undefined" && old != this) {
old.set('isEdited', false);
}
this.set('parentController.currentlyEditing', this);
}
}
});
项目控制器很快消失(见here并搜索itemController
)。我会建议你做,而不是你的用例是创建一个组件,如下所示:
App.XItemComponent = Ember.Component.extend({
classNames: ['item'],
actions: {
startEdit: function(){
this.set('beingEdited', true);
this.set('disallowEdits', true);
},
finishEdit: function(){
this.set('beingEdited', false);
this.set('disallowEdits', false);
}
}
});
<script type="text/x-handlebars" data-template-name="components/x-item">
{{ item }}
<button {{ action "startEdit" }} {{ bind-attr
disabled=disallowEdits}}>Edit</button>
{{#if beingEdited}}
<button {{ action "finishEdit" }}>Save</button>
{{/if}}
</script>
disallowEdits
是一个属性,组件将与所有其他组件共享(通过在成分为模板的控制器创建)和beingEdited
是不与任何人共享的本地组件属性。
然后,您可以使用该组件的模板里面如下:
<script type="text/x-handlebars" data-template-name="index">
<ul>
{{#each item in model}}
<li>{{x-item item=item disallowEdits=disallowEdits }}</li>
{{/each}}
</ul>
</script>
全部工作示例here
我不认为,这是可能的。你的用途是什么? – mavilein 2013-03-12 20:12:17
我在'itemController'中存储了isEditing状态,但是我想只允许一次编辑一个项目。我想我会通过他们的兄弟'itemController's循环,并将其设置为false。 – th3mus1cman 2013-03-12 21:00:00
我会让主控制器处理跟踪哪个控制器处于编辑模式,将当前编辑控制器缓存在'currentlyEditing'属性中,并手动处理在前一个itemController上关闭“isEditing”,并在传入itemController上关闭。 – 2013-03-12 21:10:20