Ember.js-为目标应用程序创建动态filterProperty值

Ember.js - creating dynamic filterProperty values for goal app

本文关键字:动态 filterProperty 创建 应用程序 js- 目标 Ember      更新时间:2023-09-26

首先,让我在序言中说,我对Ember.js是全新的,对软件开发相对较新。我正在尝试构建一个活动日志应用程序,用户可以在其中发布类似于twitter或facebook的小文本更新,也可以创建每周目标,并可以在发布更新时从目标下拉列表中选择,将帖子"应用"到目标。除了计算用户为特定目标"应用"帖子的次数外,我已经能够让一切正常工作。下面是代码的精简版本:(此外,我还制作了一个jsfiddlehttp://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函数中放入一个静态目标名称值时,(var值)timesThisWeek计数适用于该特定目标。但是,如果我试图为handlers迭代器(var value2)创建并添加一个"this",我就无法使timesThisWeek实现相关目标。

我相信这是非常基本的,但任何帮助都将不胜感激!!!

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

在模板中:

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

视图的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/