0
votes

Here's a basic post/comments.

When I create new comments, they're added with a post: attr() that matches the id of the post with which they relate to. I want to make a filter so that when choosing a specific post and navigating to the comments section only the related comments load.

The reason for such a roundabout way is that I've been unable to add an embedded comment while using either ember-model or ember-data + the respective firebase adapter.

I've also been unable to save a new comment and have that add a reference into comment_ids: [] of App.Post when created.

If anyone has an idea as how to do either of the above that would work as well.

App.Router.map(function () {
  this.resource("posts", {
    path: "/posts"
  });
  this.resource("post", {
    path: "/:post_id"
  }, function () {
    this.resource("comments", {
      path: "/comments"
    });
  });
});

App.Post = Ember.Model.extend({
  name: attr(),
  comments: Ember.hasMany("App.Comment", {
    key: 'comments'
  })
});

App.Post.url = "/posts";

App.Comment = Ember.Model.extend({
  message: attr(),
  post: attr()
});
1

1 Answers

1
votes

You best bets for filtering are Ember.computer.filter and Ember.computed.filterBy

http://emberjs.com/api/#method_computed_filter http://emberjs.com/api/#method_computed_filterBy

filterBy is the easiest, but only useful for simple single property/value filters.

App.Hampster = Ember.Object.extend({
  remainingChores: Ember.computed.filterBy('chores', 'done', false)
});

var hampster = App.Hampster.create({chores: [
  {name: 'cook', done: true},
  {name: 'clean', done: true},
  {name: 'write more unit tests', done: false}
]});
hampster.get('remainingChores'); // [{name: 'write more unit tests', done: false}]

filter allows you to pass a function for more complex filtering.

App.Hampster = Ember.Object.extend({
  remainingChores: Ember.computed.filter('chores', function(chore) {
    return !chore.done;
  })
});