EmberFire Sorting Posts


#1

I asked this on stack overflow and it didn’t get any traction, so I’m turning to here. I am trying to post my blogs in order from more recent to last. I am able to sort by title and author, but when I sort by “published” or “publishedDate” the sort doesn’t take. I have also tried turning the sortAscending/sortDescending in my controller to true/false as well as messing with the “limitToFirst/Last” in my route. Here is my code:

 template/blog.hbs
 
  <h2>Below are your Latest Blog Posts</h2>

 	<div class="col-sm-8">
	{{#each model as |post|}}
	    <div id=blogPost>
<div id='published'><p>Published:  {{post.publishedDate}}</p></div
<div id='title'><p>Title:  {{post.title}}</p></div>
		<div id='author'><p>Author:  {{post.author}}</p></div>
		<div id='body'><p>Post:  {{post.post}}</p></div>
	</div>
	{{/each}}
</div>
 </div>

controllers/posts.js

import Ember from 'ember';

 export default Ember.Controller.extend({
  sortProperties: ['published'],
 sortAscending: false, 
   actions: {
     publishPost: function() {
       var newPost = this.store.createRecord('post', {
       title: this.get('title'),
      post: this.get('post'),
   author: this.get('author'),
     published: new Date().getTime()
  });

  newPost.save();
    }
  }
});

models/post.js

 import DS from 'ember-data';

 export default DS.Model.extend({
  title: DS.attr('string'),
    author: DS.attr('string'),
   post: DS.attr('string'),
  published: DS.attr('number'),
 publishedDate: Ember.computed('published', function() {
   return moment(this.get('published')).format('MMMM Do, YYYY');
  })
});

routes/blog.js

 import Ember from 'ember';

  export default Ember.Route.extend({
    model: function() {
     return this.store.query('post', {
	orderBy: 'published',
	limitToFirst: 10
   });
  }
 });

#2

You can use Ember.computed.sort like this: