2013-04-02 36 views
1

我有一篇帖子,其中有很多評論。問題是我能夠獲取所有評論,但我無法獲取單個評論,因此無法編輯單個評論。由於我無法獲取單個評論,這意味着我無法將單個記錄添加到交易或編輯單個記錄。無法編輯燼數據中的hasMany關聯子記錄

評論是sideloaded,不會支持的路線,我不想路線的任何評論相關的控制器。所以我在ApplicationRoute中使用controllerFor設置CommentController的模型,然後使用[needs] api將模型包含在可能需要模型內容的其他評論相關控制器中。

您可以通過點擊後到達評論 - >然後單擊文章標題 - > 點擊新增評論 *然後保存並reclick editcomment

這是jsfiddle,但與此相關的問題代碼的相關位下方是:

EmBlog.ApplicationRoute = Ember.Route.extend({ 
    setupController: function() { 

    this.controllerFor('comment').set('model', EmBlog.Comment.find());  
    } 
}); 

的評論控制器

//Even when I use ArrayController, the error is still there 
    EmBlog.CommentController = Ember.ObjectController.extend({ 
     content: null 
    }); 

的控制器手柄編輯,所有的錯誤發生在editComment方法

EmBlog.CommentEditController = Ember.ObjectController.extend({ 

    needs: ['comment'], 
    isEditing: false, 

    editComment: function(post) { 

    var comment = this.get('controllers.comment.content'); 

    var yk = comment.get('post'); 

    //this line is undefined 
    console.log(yk); 

    var commentEdit = this.set('content', comment); 
    console.log(commentEdit); 

    transaction = this.get('store').transaction(); 

    //Uncaught Error: assertion failed: You must pass a record into transaction.add() 
    transaction.add(commentEdit); 

    this.transaction = transaction; 

    this.set('isEditing', true); 

    } 

    }); 

把手的職位/顯示

<script type="text/x-handlebars" data-template-name="posts/show"> 
    {{render 'comment/edit' comment}} 
    </script> 

回答

0

現在一切都working and here is the jsfiddle,我能夠在燈具編輯的意見。

但唯一存在的問題是,我只能編輯夾具中的第一個註釋。我無法編輯第二條評論或無法編輯任何新評論的新評論,我添加了

我基本上通過需求api在postShow中獲取帖子,然後獲取帖子的id並將其作爲查找參數傳遞給** EmBlog.Comment.find **。然後我將EmBlog.CommentEditController的內容設置爲我們剛纔所做的結果。在此之後,我添加的EmBlog.CommentEditController新的內容到交易與transaction.add(this.get( '內容'))

EmBlog.CommentEditController = Ember.ObjectController.extend({ 

    needs: ['comment', 'postsShow'], 
    isEditing: false, 

    editComment: function() { 
    var post = this.get('controllers.postsShow.content'); 
    console.log(post); 

    var postId = post.get('id'); 
    console.log(postId); 

    comment = EmBlog.Comment.find(postId); 
    console.log(comment); 

    transaction = this.get('store').transaction(); 

    var updatedContent = this.set('content', comment); 
    console.log(updatedContent); 
    console.log(this.get('content')); 

    transaction.add(this.get('content')); 
    console.log(transaction); 

    this.transaction = transaction; 

    this.set('isEditing', true); 

    }, 

    save: function(){ 

    var comment = this.get('content'); 
    comment.one('didUpdate', this, function() { 
     this.set('isEditing', false); 
    }); 

    this.transaction.commit(); 
    this.transaction = null; 
    console.log(this.get('content')); 
    } 

});