【问题标题】:Ember push record in model and update template模型中的 Ember 推送记录和更新模板
【发布时间】:2023-03-25 07:06:02
【问题描述】:

我的 Ember 应用中有这条路线:

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 组件负责调用 newComment 操作:

// 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);

我需要的是能够动态地添加新的本地评论(无需将其保留在服务器上)并更新模板以显示新添加的记录而无需完整的页面刷新

【问题讨论】:

  • 请同时发布您的模板的 sn-p,以便更容易为您提供帮助
  • 请同时发布您定义的comment 模型
  • @mihai 我已经用请求的信息更新了问题

标签: ember.js ember-data


【解决方案1】:

它应该像这样工作:

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 
}

这仅在您确实有 cmets 时才有效。如果没有,您首先需要将您的 cmets 初始化为一个空数组。否则:

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 
    }
}

【讨论】:

  • 很遗憾没有;这样做我得到错误“未定义的pushObject”
  • 更新了答案。我不知道您在哪个范围内调用动作
  • 另外,如果 cmets 不为空,我在尝试推送模型中的新记录时会收到错误“Uncaught TypeError: internalModel.getRecord is not a function”
  • 第一个片段的最后一行看不懂。为什么我需要将model.comments 重置为它已经拥有的相同值?此外, cmets 永远不应为空;如果没有,query 将返回一个空数组。
【解决方案2】:

制作 cmet 列表的可修改副本并将其保存在控制器上:

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|}}

【讨论】:

  • 好的,指出查询结果不可写;在任何情况下“this.get('cmets').pushObject(record) 不起作用;我已将其更改为正确的“this.get('controller').get('cmets').pushObject(record)更新数组,但此后模板不更新
  • 好吧,我不知道动作是在哪里定义的。我以为它在控制器上。从路由器操纵控制器是一种反模式。因此,我会将动作移至控制器。我不太确定这是否能解决模板不更新的问题。您确定已将模板固定为循环遍历 comments 而不是 model.comments
  • 是的,我改变了模板的循环;但是在 ember 2.x 中不推荐使用控制器吗?无论如何,让模板更新以显示新评论是我的主要目的
  • 不,它们并没有被弃用。它们是任何 Ember 应用程序不可或缺的元素。 “可路由组件”有一个愿景或方向声明,可能会在 Ember 2.12 中出现,或者谁知道它们何时、何时出现。弃用在 Ember-land 中具有非常特殊的含义。这意味着一个功能现在有一个可用的替代品,您应该切换到它,如果您不这样做,您将看到一条弃用消息。
  • 好的,无论如何我已经尝试过控制器上的操作,但似乎它不会导致模板更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
相关资源
最近更新 更多