2016-01-21 93 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不是函数” –