首先,让我先说我对 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 函数中放置一个静态目标名称值时,(var value) timesThisWeek 计数适用于该特定目标。但是,如果我尝试为车把迭代器(var value2)添加一个“this”,我不能让 timesThisWeek 为相关目标工作。
我敢肯定这是非常基本的,但任何帮助将不胜感激!!!