我有这个 Controller ,可以从服务器获取数据,我必须触发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);
});
}
});
问题是记录被添加到列表的底部。我希望它像原生 js unshift
一样工作,但现在它像 push
一样工作。寻找类似 unshiftObject
的东西来使添加的对象成为数组中的第一个对象。
最佳答案
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
});
关于ember.js - ember js如何将对象推送到ArrayController中的堆栈顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25428445/