首先,让我先说一下我是Ember.js的新手,并且是软件开发的新手。我正在尝试构建一个活动日志记录应用程序,在该应用程序中,用户可以发布类似于twitter或facebook的小文本更新,还可以创建每周目标,还可以通过在发布更新时从目标下拉列表中进行选择,将发布“应用于”目标。除了计算用户“将”帖子“应用”到特定目标的次数之外,我已经能够完成所有工作。因此,这是下面的代码的简化版本:(此外,我将jsfiddle http://jsfiddle.net/jjohnson/KzK3B/3/放在一起)

App = Ember.Application.create({});

App.Goal = Ember.Object.extend({
    goalTitle: null,
    timesPerWeek: null

});

App.Post = Ember.Object.extend({
    postContent: null,
    goal_name: null
});

App.postsController = Ember.ArrayController.create({
  content: [
      App.Post.create({ postContent: "went and played golf today, shot 69", goal_name: "Play a round of golf" }),
      App.Post.create({ postContent: "went and played golf today, shot a 70", goal_name: "Play a round of golf" }),
      App.Post.create({ postContent: "went to the range for an hour", goal_name: "Range practice" })
  ]




});

App.goalsController = Ember.ArrayController.create({
    content: [
        App.Goal.create({ goalTitle: 'Play a round of golf', timesPerWeek: 2 }),
        App.Goal.create({ goalTitle: 'Range practice', timesPerWeek: 3 })
                        ],

    timesThisWeek: function() {
            var value = "Play a round of golf";
            var value2 = this.goalTitle;
        return App.postsController.filterProperty('goal_name', value).get('length');
      }.property('@each.goal_name')

});


因此,我认为我已经接近解决这个问题,但是即使我确定自己正在忽略一些非常简单的事情,也还是无法完全理解。当我在timesThisWeek函数中放置一个静态目标名称值时,(可变值)timesThisWeek计数适用于该特定目标。但是,如果我尝试为车把迭代器(var value2)添加一个“ this”,则无法使timesThisWeek达到相关目标。

我敢肯定这是非常基本的,但是任何帮助将不胜感激!!!

最佳答案

使用CollectionView使它运行,并在视图中移动项目的绑定过滤@ http://jsfiddle.net/MikeAski/KzK3B/5/

在模板中:

{{view Ember.CollectionView contentBinding="App.goalsController" itemViewClass="App.GoalView"}}


View的JS:

App.GoalView = Ember.View.extend({
    templateName: "goal-view",
    timesThisWeek: function() {
        return App.postsController.filterProperty('goal_name', Ember.getPath(this, "content.goalTitle")).get('length');
    }.property('App.postsController.content.@each')
});


但是您也可以将此属性作为成员或Goal模型移动(这取决于您的实际用例:我想您对结果的时间表也不同,等等)。

编辑

最新版本:http://jsfiddle.net/MikeAski/z3eZV/

10-05 20:30
查看更多