过滤和重新排列Ember控制器中的模型/内容



假设我有一个JSON数据数组,类似于:

[ {"name":"parijat","age":28},
  {"name":"paul","age":28},
  {"name":"steven","age"27},
...
] 

这是我模型的一部分,这个模型是这样设置的:

App.UserRoute=Ember.Route.extend({模型:函数(){return App.User.FIXTURES;//上文定义}});

我想从这个JSON数组中获得唯一的年龄,并将其显示在我的模板中,所以我参考了计算属性的文章,并阅读了一些关于如何枚举Ember Enumerables的内容,最终得到:

    App.UserController = Ember.ArrayController.extend({
         ages:function(){
              var data = this.get('content'); 
              var ages = data.filter(function(item){
                       return item.age;
             });
        }.property('content');
});

现在,上面的控制器代码是不正确的,但问题是,如果我在里面添加console.log语句,它甚至不会进入data.filter()方法。IMO,它通常应该是控制台日志记录的次数与存在App.Users.FIXTURE的次数一样多。我尝试使用属性('content.@each'),但也不起作用。也没有将this.get('content')更改为this.get('content.@each')this.get('content.@etch').toArray(){抛出错误}。

不知道在这里该做什么,也不知道我完全错过了什么。

Filter用于减少项目数量,而不是映射。

你可以使用data.mapBy("年龄")来获得你的年龄数组:

ages:function(){
  return this.get('content').mapBy('age');
}.property('content.@each')

或者在你的车把功能中,你可以只使用每个助手:

{{#each item in controller}}
  {{item.age}}
{{/each}}

最新更新