First, let me preface this by saying that I'm brand-new to Ember.js and relatively new to software development. I'm trying to build an activity logging application where users can post up small text updates similar to twitter or facebook, can also create weekly goals and can optionally "apply" posts to goals by selecting from a goal drop-down when posting updates. I've been able to get everything to work, except for counting how many times a user has "applied" a post to a specific goal. So here's a stripped down version of the code below: (Also, I put together a 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')
});
So, I think I've come close to figuring this out, but can't quite get it even though I'm sure I'm overlooking something incredibly simple. When I put a static goal name value in the timesThisWeek function, (var value) the timesThisWeek count works for that specific goal. But if I try to create add a "this" in for the handlebars iterator (var value2), I can't make timesThisWeek work for the relevant goal.
I'm sure this is very basic, but any help would be greatly appreciated!!!
Got it running using CollectionView
, and moving the item's bound filtering in the view @ http://jsfiddle.net/MikeAski/KzK3B/5/
In the template:
{{view Ember.CollectionView contentBinding="App.goalsController" itemViewClass="App.GoalView"}}
View's 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')
});
But you may also move this property as a member or the Goal
model (it depends on your real use case: I presume you also have varying time frame for result, etc.).
EDIT
Last version: http://jsfiddle.net/MikeAski/z3eZV/
self
was a rest... :-P
2) Well, as said, it depends on the full context, but personally, I would put it into the model, as it is a good place to store business logic (which timesThisWeek
almost is) - Mike Aski 2012-04-05 05:51
You've got the whole thing working. You're welcome (maybe you could consider voting this reply, if it fits your needs...) - Mike Aski 2012-04-05 12:08