Ember.js-为目标应用程序创建动态filterProperty值
Ember.js - creating dynamic filterProperty values for goal app
首先,让我在序言中说,我对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/
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 动态分配GA增强型电子商务跟踪器
- onkeyup无法动态创建多个文本区域
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何使Javascript动态html表及其上的事件
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 如何使用javascript从主svg对象动态创建svg视图框
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- 如何在Google柱状图中动态添加行/列
- 困在逻辑中试图定位动态的东西
- 在动态创建的元素上获取对特定选择器的引用
- 如何动态插入jquery代码
- 使用Google Visualization动态调用构造函数
- 使用jQuery动态添加表并在其中动态添加行
- d3基于用户选择动态更新节点
- 使用JQuery的动态上下文菜单
- JQuery对动态创建的对象进行选择