ember js如何将对象推送到ArrayController中的堆栈顶部

Ale*_*hev 2 ember.js ember-model

我有这个控制器从服务器获取数据我必须触发fetchPosts方法并添加新的火灾后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);
      });
   }
});
Run Code Online (Sandbox Code Playgroud)

问题是记录正在添加到列表的底部.我希望它像本机js一样工作,unshift但现在它的工作方式就像push.寻找类似于unshiftObject使添加的对象成为数组中的第一个对象的东西.

Kin*_*n2k 5

unshiftObject适用于Ember http://emberjs.com/api/classes/Ember.MutableArray.html#method_unshiftObject.

您的代码有一个超出范围的问题:

  var post = this.get('newPost');
  post.save().then(function () { 
    this.pushObject(post); // <----- this this wouldn't be the array controller
  });
Run Code Online (Sandbox Code Playgroud)