2014-02-05 2 views
0

나는 PostComment의 두 모델이 있습니다. 둘 다 자신의 경로가 있습니다 : PostRoutePostCommentsRoute 첫 번째 경우에는 단순히 모델 데이터를로드하는 데 @store.find을 사용하지만 두 번째 경로에는 문제가 있습니다. Post을 참조하고 주석을 가져 오려고하면로드되지만로드 할 때마다 업데이트되지 않습니다. @store.find 'comment'을 사용하면 업데이트가 유지되지만 모든 것이로드되므로 중요하지 않습니다. 게시물과 관련된 댓글 만 받고 업데이트 된 상태로 유지하는 방법은 무엇입니까?올바르게 얻는 방법 hasMany route in

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 

답변

1

당신은 저장 한 후 당신의 comment 당신은 post에 밀어해야 님의 댓글 : (나는`썩에`comments`을 변경했다

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); 
    }); 
}); 
+0

PostCommentsRoute에 댓글 목록을 업데이트하려면 ' 콘텐츠 ')'하지만 그것은 해결책입니다. –

관련 문제