我有以下路线和控制器:
var App = App || Ember.Application.create();
App.Router.map(function(){
this.resource('posts', function(){
this.route('new', { path: '/new'});
});
});
App.PostsRoute = Ember.Route.extend({
model: function () {
return $.getJSON('/api/posts');
},
setupController: function(controller, model) {
$.getJSON('/api/posts').done(function(data) {
controller.set('model', data);
});
});
App.PostsNewController = Ember.Controller.extend({
actions: {
addPost: function (file) {
var self = this;
$.ajax({
type: 'POST',
url: '/api/posts',
data : {
id: this.get('id'),
title: this.get('title'),
author: this.get('author'),
contents: this.get('contents')
}
}).done(function(res){
self.transitionToRoute('posts');
});
}
}
});
我想做的是在用户成功保存新帖子后调用“帖子”路由以刷新浏览器中的帖子列表。
因此,我使用“ self.transitionToRoute('posts')”在PostsRoute中调用模型挂钩或setupController,但从未调用过这两种方法。
我究竟做错了什么?提前致谢。
由于您已经在发布路线中,因此Ember不需要重新获取模型。另外,控制器已经设置好,并且已经有了模型,因此也没有理由调用它。本质上是避免浪费电话。就个人而言,我建议您也避免添加回调(除非您期望其他用户发布其他信息)。
needs: ['posts'],
actions:{
addPost: function (file) {
var self = this,
data = {
id: this.get('id'),
title: this.get('title'),
author: this.get('author'),
contents: this.get('contents')
};
$.ajax({
type: 'POST',
url: '/api/posts',
data : data
}).done(function(res){
// just push the new data
self.get('controllers.posts').pushObject(data);
self.transitionToRoute('posts');
});
}
}
actions:{
addPost: function (file) {
var self = this,
data = {
id: this.get('id'),
title: this.get('title'),
author: this.get('author'),
contents: this.get('contents')
};
$.ajax({
type: 'POST',
url: '/api/posts',
data : data
}).done(function(res){
self.send('updatePosts');
self.transitionToRoute('posts');
});
}
}
App.PostsRoute = Ember.Route.extend({
model: function () {
return $.getJSON('/api/posts');
},
actions: {
updatePosts: function(){
var controller = this.get('controller');
this.model().then(function(data){
controller.set('model', data);
});
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句