在1.13中动态地将js对象添加到模型数组中 [英] Dynamically add js object to model array in 1.13

查看:90
本文介绍了在1.13中动态地将js对象添加到模型数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

var msg = this.store.createRecord({text:'first title', createdAt: "2015-06-22T20:06:06+03:00" })

this.get('model.content').pushObject(msg);

msg.save();

我们创建新记录。然后将其推入模型显示。它在1.9版本中完美工作,但升级到最新的1.13之后,它会中断并显示此错误:

We create new record. Then push in it to the model to display. It worked perfectly in 1.9 version but after upgrading it to the newest 1.13 it breaks and shows this error:


TypeError:internalModel.getRecord不是一个函数

TypeError: internalModel.getRecord is not a function

经过一些研究,我出来了解决方案

after some researches I came out to this solution

this.get('messages.content').unshiftObject(message.internalModel);

,它部分帮助。现在我有两个问题:

and it partially help. Now I have two problems:


  • 我不确定如果使用私人垃圾数据api是个好主意

  • 我向模特添加记录和
    在屏幕上呈现一个烦人的延迟。如果我不叫
    msg.save();该记录未被呈现。所以据我所知,
    等待,直到我们从服务器得到响应,然后再呈现。
    但是我需要相反的行为 - 我需要首先显示记录,然后
    然后保存它(显示用户的保存状态),这样用户
    认为一切都会快速地进行。 / li>
  • I'm not confident if using private ember data api is a good idea
  • I have an annoying delay between adding record to the model and rendering it on the screen. More than that if I don't call msg.save(); the record isn't rendered. So as far as I understood it waits until we have response from server and only then renders it. But I need opposite behaviour - I need to show the record first and then save it(showing the saving state for the user), this way user thinks that everything goes extrimely fast.

推荐答案

不使用私有API的一个可能的解决方案是使用 toArray () github issue ):

One possible solution without resorting to private API is to use toArray() (github issue):

var array = this.get('messages').toArray()
array.addObjects(this.get('messages'))
array.addObject(msg)
this.set('messages', array)

这篇关于在1.13中动态地将js对象添加到模型数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆