2012-04-04 35 views
2

首先,让我通过说我对Ember.js是全新的,并且相对较新的软件开发来说明这一点。我正在尝试构建一个活动日志记录应用程序,用户可以在其中发布类似于Twitter或Facebook的小文本更新,还可以创建每周目标,并可以在发布更新时从目标下拉列表中选择“应用”发布到目标。除了统计用户将帖子“应用”到特定目标的次数之外,我已经能够让所有内容都能够正常工作。因此,这里的下面的代码的精简版:(另外,我放在一起的jsfiddle http://jsfiddle.net/jjohnson/KzK3B/3/Ember.js - 为目标应用程序创建动态filterProperty值

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计数适用于该特定目标。但是,如果我尝试为handlebars迭代器(var value2)添加“this”,我无法使timesThisWeek为相关目标工作。

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

回答

1

得到它运行使用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('[email protected]') 
}); 

但是你可能还移动此属性作为成员或Goal模型(这取决于您的实际使用情况:我认为你也有有不同的结果时间框架等)

编辑

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

+0

真棒,感谢您的快速反应!出于某种原因,我无法将其集成到当前的应用程序中并正常工作,但我会继续努力。另外,还有几个简单的问题:1)为什么使用self = this?我已经看了几次,但还不知道为什么它很有用。 2)我正在考虑让timesThisWe离开Goal模型,因为如果它不在模型中,我认为可能会更容易地过滤另外的时间,但是您是否认为这将是一个明智的决定,在目标模式本身?我打算使用ember-data和rails来实现持久化,并希望得到最好的解决方案 – 2012-04-05 00:11:17

+0

1)Ho ...是的,'self'是休息... :-P 2)那么,如上所述,这取决于在个人背景下,但我个人而言,我会把它放到模型中,因为它是存储业务逻辑的好地方(这个'timesThisWeek'差不多是)。 – 2012-04-05 05:51:01

+0

感谢您的回应!我更新了JS小提琴 - 我似乎无法在添加新帖子时正确触发绑定。 http://jsfiddle.net/jjohnson/sbytv/1/当单击“添加帖子”按钮时,timesThisWeek不会更新。我试过@ each.goal_name等等,但没有任何工作。有任何想法吗? – 2012-04-05 05:58:01