我有2个模型Post
和Comment
。两者都有自己的路由:PostRoute
和PostCommentsRoute
,在第一种情况下,我只是使用@store.find
来加载模型数据,但我对第二条路线有问题。当我尝试引用Post
然后得到它的注释时,它们会加载但是在我添加新注释时不会更新。如果我使用@store.find 'comment'
,它会保持更新,但会加载所有内容,所以这不是重点。如何只获得与帖子相关的评论并保持更新?
App.Post = DS.Model.extend
comments: DS.hasMany 'comment', async: true
App.Comment = DS.Model.extend
post: DS.belongsTo 'post'
text: DS.attr 'string'
App.Router.map ()->
@resource 'post', path: '/post/:post_id', ()->
@route 'comments'
App.PostCommentsController = Em.ArrayController.extend
newComment: ""
needs: 'post'
post: Em.computed.alias 'controllers.post'
sortProperties: ['created'],
sortAscending: false
actions:
addComment: ()->
commentText = @get 'newComment'
post = @get 'post'
comment = @store.createRecord 'comment',
text: commentText
post: post.get 'content'
@set 'newComment', ''
comment.save()
App.PostRoute = Em.Route.extend
model: (params)->
@store.find 'post', params.post_id
App.PostCommentsRoute = Em.Route.extend
model: (params)->
# loads only comments to parent post but doesn't update
post = @modelFor 'post'
post.get 'comments' # /api/posts/1/comments
App.PostCommentsRoute = Em.Route.extend
model: (params)->
# updates but loads all comments
@store.find 'comment' # /api/comments
答案 0 :(得分:1)
保存comment
后,您必须按post
的评论推送它:
comment.save().then(function () {
post.get('comments').push(comment);
// or if that doesn't work you may need to resolve the promise:
post.get('comments').then(function (comments) {
comments.push(comment);
});
});