更新自从提出这个问题以来,我重新设计了我的 UI,这样我就不再需要这个功能了;但是,为了帮助遇到类似问题的其他人,我将保持开放和积极的态度。
我在模板中列出了一组元素,每个元素都有一个链接,可以在列表右侧打开它。单击一个时,我只想隐藏该元素并在单击另一个时再次显示它。我目前执行此操作的方法是在模型上将属性(活动)设置为 true。这感觉不对,原因有以下三个:
- 这个属性实际上不是模型模式的一部分,它只是任意的;这使它看起来像是一个控制器问题(请参阅下文了解为什么这不起作用)
- 我必须首先在所有型号上将 active 设置为 false,迫使我更改另一个路由器的型号,这可能是好是坏,我不确定
- 在最近的 PeepCode 截屏视频中,他展示了使用
@each.{attribute}
绑定到数组中的属性;这让我觉得必须有类似的事情我可以做(比如this.set("@each.active", false)
)一举将它们全部设置好
我想在控制器上使用一个方法,但似乎我不能将参数传递给 Handlebars if 语句中的函数。
这是我用来呈现列表的代码:
{{#each note in controller}}
{{!v-- I was trying to do {{#if isCurrentNote note}} but that seems to be invalid handlebars}}
{{#unless note.active}}
<li class="sticky-list-item">
{{view Ember.TextArea classNames="sticky-note" valueBinding="note.content"}}
{{#linkTo note note classNames="sticky-permalink"}}
∞
{{/linkTo}}
</li>
{{/unless}}
{{/each}}
以下是路线:
App.NotesController = Ember.ArrayController.extend({
// v-- this is what I was trying to do, but couldn't pass note in the template
isCurrentNote: function(note){
return this.get("currentNote") == note;
}.property("currentNote")
});
App.NoteRoute = Ember.Route.extend({
setupController: function(controller,model){
this.modelFor("notes").forEach(function(note){
note.set("active", false);
});
model.set("active", true);
}
});
就像我说的,我有什么工作,但感觉不对。谁能证实我的怀疑或帮助我放松一下?
谢谢!