0

I have a question:

Assuming I have a 'users' property in the controller which contains a list of user objects, then in the template, I would like to do something like: (currentUser is just a property in the ApplicationController available in any template)

each u in users if u.id is currentUser.id renderThisBlock

Obviously 'if' helper can't work like that, it seems like Ember doesn't support block helpers so i'm not sure how this could be achieved,

please note that I need the condition to be per iteration, otherwise I would expose a method in the controller that returns boolean.

Thanks.

4

2 回答 2

0

您可以itemController{{#each}}块中使用

只要有

{{#each u in users itemController="App.TestController"}} 
  {{#if someComputedProperty}}
     renderThisBlock
  {{/if}}
{{/each}}

您可以定义您的 itemController 来检查 userId 和 currentUserId 为

App.TestController = Em.ObjectController.extend({
   someComputedProperty: function(){
     var currentUserId = this.get('target).get('controllers.application.currentUserId');
     return (this.get('content.userId) === currentUserId)? true : false;
   }
});

将需求放入 itemController 看起来效率不高。所以让你的应用程序控制器通过needs你的上下文控制器(我的意思是你的模板的上下文)

 App.AnotherController = Em.ArrayController.extend({
   needs: ['application']
 });

希望这能完成你的工作

于 2013-07-29T16:50:50.057 回答
0

谢谢,

有两种选择:

可以为每个这样的部分创建一个视图,然后可以将这样的方法添加到每次迭代都会有一个上下文的视图中

第二个是上面的解决方案,但是在迭代中的一个更改应该指定控制器的短名称而不是全局命名空间中的全名,例如:

{{#each u in users itemController="test"}} 

这绑定到 App.TestController。

谢谢!

于 2013-07-29T18:37:01.980 回答