Dynamically add js object to model array in 1.13

六月ゝ 毕业季﹏ 提交于 2020-01-12 11:45:54

问题


I have the following code:

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

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

msg.save();

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

  • 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.

回答1:


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)



回答2:


Before 1.13:

this.get('content').pushObjects(messages);

After 1.13:

messages.forEach(functio(message) {
  this.get('model.content').pushObject(message._internalModel);
}); 



回答3:


I would make all return models to array then, add array to this array

setPeople:function(){
    this.set('people',this.get('content').toArray())
}.observes('content')

then, find more person models, to array

getMoreUsers(){
      var self = this;
            this.set('offset', this.get('offset')+1);
            self.store.findAll('person').then(function(people){
             self.get('people').pushObjects(people.toArray());
            });


来源:https://stackoverflow.com/questions/30990198/dynamically-add-js-object-to-model-array-in-1-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!