Ember.js - creating dynamic filterProperty values for goal app

Go To StackoverFlow.com

2

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!!!

2012-04-04 07:55
by Jack Johnson


1

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/

2012-04-04 08:14
by Mike Aski
Awesome, thanks for the quick response! For some reason I can't get this integrated into my current app and working correctly, but I'll keep working on it. Also, a couple quick questions: 1)Why use self = this? I've seen this a few times but don't yet know why it's useful. 2)I was thinking of keeping timesThisWeek out of the Goal model because I thought it might be easier to filter additionally by time if it wasn't in the model, but do you think it would be a wise decision to deal with it in the Goal model itself? I'm going to use ember-data and rails for persistence and want the best solutio - Jack Johnson 2012-04-05 00:11
1) Ho... yes, 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

Thanks for the response! I updated the JS fiddle - I can't seem to get the bindings to fire correctly when a new post is added. http://jsfiddle.net/jjohnson/sbytv/1/ When the "Add Post" button is clicked, timesThisWeek aren't updated. I tried @each.goal_name and so forth but nothing worked. Any ideas - Jack Johnson 2012-04-05 05:58
Updated sample here: http://jsfiddle.net/MikeAski/z3eZV/

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

Ads