2016-01-21 61 views
0

我在灰燼應用這條路線:餘燼推記錄

model: function(params, transition) { 
    var self = this; 

    return Ember.RSVP.hash({ 
     photo: self.store.find('photo', params.id), 
     comments: self.store.query('comment', {id: params.id}) 
    }); 
}, 

actions: { 
    newComment: function(comment) { 
     var record = this.store.createRecord('comment', comment); 

    } 
} 

模板:

{{#each model.comments as |comment|}} 
    <div class="card"> 
     <div data-userId="{{comment.userId}}"> 
      <b>{{comment.username}}</b> 
     </div> 
     <div> 
      {{comment.content}} 
     </div> 
     <span class="hide-on-small-only">{{i18n 'createdAt_lbl'}}: </span>{{format comment.createdAt type='date'}} 
    </div> 
{{/each}} 

{{post-comment newComment='newComment' comments=model.comments}} 

和註釋模式:

export default DS.Model.extend({ 
    commentHash: DS.attr('string'), 
    content: DS.attr('string'), 
    createdAt: DS.attr('date'), 
    username: DS.attr('string'), 
    userHash: DS.attr('string'), 
    userId: DS.attr('number'), 
}); 

的後評論組件是負責調用新評論操作的人員:

// post-comment component 
var self = this; 

// get the new comment content from textarea 
var $contentArea = this.$('#postCommentContent'); 
var content = $contentArea.val(); 

var newComment = { 
    userId: localStorage.getItem('userId'), 
    content: content, 
    createdAt: moment().format('MMMM Do YYYY, h:mm:ss a') 
}; 
self.sendAction('newComment', newComment); 

我需要的是能夠添加新的本地評論(不堅持在服務器上)dinamically,使模板更新,以顯示新添加的記錄不完整的頁面刷新

+0

請發表您的模板的片段,以及因此它更容易以幫助您 – mihai

+0

也請張貼您已定義的'comment'模型 – mihai

+0

@mihai我已更新了所需信息的問題 –

回答

1

發表的評論列表的修改副本,並保持它的控制器上:

setupController(controller, model) { 
    this._super(...arguments); 
    controller.set('comments', model.comments.toArray()); 
} 

你需要做一個拷貝是從store.query返回值是,對於原因各種原因,不可寫。可能有其他方法來製作副本,但toArray似乎很好。

添加新的評論到此列表創建後:

actions: { 
    newComment: function(comment) { 
    var record = this.store.createRecord('comment', comment); 
    this.get('comments').pushObject(record); 
    } 
} 

在模板中,環比控制器屬性:

{#each comments as |comment|}} 
+0

ok thans指出查詢結果是不可寫的;在任何情況下,「this.get('comments')。pushObject(記錄)不起作用;我已經將它改爲」this.get('controller').get('comments')。pushObject更新陣列,但模板不更新之後 –

+0

嗯,我不知道在哪裏定義了操作。我認爲它在控制器上。從路由器操縱控制器是一種反模式。因此,我會將行動移至控制器。我不太確定這是否可以解決模板未更新的問題。你確定你修改了模板來循環使用'comments'而不是'model.comments'嗎? – 2016-01-21 17:50:48

+0

是的,我改變了模板的循環;但不是在ember 2.x中不贊成使用控制器?在任何情況下,使模板更新顯示新的評論是我的主要目的 –

0

它應該簡單地這樣的工作:

newComment: function(comment) { 
    var record = this.store.createRecord('comment', comment); 
    var commentsModel = this.get('model.comments'); // or this.get('comments'), depending where you action resides 
    commentsModel.pushObject(record); 
    this.set('model.comments', commentsModel); // or this.set('comments'), depending where you action resides 
} 

這隻適用於如果你真的有意見。如果不是,您首先需要將您的評論初始化爲空數組。否則:

newComment: function(comment) { 
    var record = this.store.createRecord('comment', comment); 
    var commentsModel = this.get('model.comments'); // or this.get('comments'), depending where you action resides 
    if(!commentsModel){ 
     commentsModel = Ember.A(); 
    } 
    commentsModel.pushObject(record); 
    this.set('model.comments', commentsModel); // or this.set('comments'), depending where you action resides 
    } 
} 
+0

不幸的是,這樣做我得到了錯誤「undefined的pushObject」 –

+0

更新了答案。我不知道你在哪個範圍內調用動作 –

+0

另外,如果註釋不是空的,當試圖推動模型中的新記錄時,我得到錯誤「Uncaught TypeError:internalModel.getRecord不是函數」 –