3

I have this controller where to fetch data from the server I have to fire fetchPosts method and to add new post fire add

App.PostsController = Ember.ArrayController.extend({
  actions: {
    fetchPosts: function (id) {
      var data = App.Post.find({category: id});
      this.set('model', data);
    },
    add: function () {
      var post = this.get('newPost');
      post.save().then(function () { 
        this.pushObject(post);
      });
   }
});

The problem is that the record are adding to the bottom of the list. I want it to work like native js unshift but now it works like push . Looking for something like unshiftObject to make added object the first object in the array.

Alex Berdyshev
  • 761
  • 2
  • 7
  • 21
  • You might want to check [this](http://stackoverflow.com/questions/15210249/ember-data-insert-new-item-at-beginning-of-array-instead-of-at-the-end/15235300#15235300). Also, read about [sortProperties](http://emberjs.com/guides/controllers/representing-multiple-models-with-arraycontroller/#toc_sorting) and the [SortableMixin](http://emberjs.com/api/classes/Ember.SortableMixin.html) that makes it possible. – MilkyWayJoe Aug 21 '14 at 14:14

1 Answers1

6

unshiftObject works in Ember http://emberjs.com/api/classes/Ember.MutableArray.html#method_unshiftObject.

Your code has an out of scope issue here:

  var post = this.get('newPost');
  post.save().then(function () { 
    this.pushObject(post); // <----- this this wouldn't be the array controller
  });
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96