如果POST响应只包含id,那么如何将数据保存到我的Ember存储? [英] How do I save data to my Ember store if the POST response contains only an id?

查看:134
本文介绍了如果POST响应只包含id,那么如何将数据保存到我的Ember存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ember数据期望我的服务器在每个成功的 POST 之后返回完整的对象。但是,我的API只返回一种包含 id 的元对象。当Ember收到此对象时,记录中的现有数据将被删除,除了 id



所以例如,当我这样做:

  var asset = App.Asset.store.createRecord('asset',{name:'foo .ai',
路径:['/path/foo.ai','/another/path/foo.ai'],
类型:'ai',
哈希:'1234567890123456789012345678901234567890 ',
datecreated:'Sun,12 Jan 2014 06:01:48 GMT',
datemodified:'Sun,12 Jan 2014 06:01:48 GMT',
thumbnail:'/ path / to / thumb.jpg'
});

asset.save();

...服务器响应如下:


$ b $

$ b更新:星期一,2014年1月13日19:49:14 GMT,
id: 52d4433a8e3b08444740ea47,
}

当Ember收到此消息时,它会更新 id 的记录与服务器响应中收到的记录,但它也删除所有其他数据,因为它不在响应。如果我在Ember帮手中检查我的数据,它看起来像这样:

  {
id:'52d4433a8e3b08444740ea47'
名称:undefined,
路径:undefined,
类型:undefined,
哈希:未定义,
日期处理:未定义,
datemodified:undefined,
thumbnail:undefined
}

如何覆盖此行为并获取Ember我们已经看过:




    DS.RESTAdapter 中钩入 createRecord ,但它返回一个承诺,所以我无法提取新的 id
  • 在<$ c中钩入 extractSave $ c> DS.RESTSerializer ,但它希望存储类型有效负载作为输入,而不是个别的记录!如果记录被传递到这里,我可以更新其 id 与有效载荷中的内容,并解决我的问题。



有没有我缺少的东西?

解决方案

看起来像现有的数据记录被覆盖在记录的 adapterDidCommit 方法中。



请参阅 https://github.com/emberjs/data/blob/ master / packages / ember-data / lib / system / model / model.js#L656-L657



一种解决方案可能是扩展该方法来保存现有数据来自记录。我相信它可能看起来像这样。

  DS.Model.reopen({
adapterDidCommit:function()
var oldData = this.toJSON();
this._super.apply(this,arguments); //调用原来的adapterDidCommit方法
var newData = this._data;
this._data = Ember.merge(oldData,newData);
}
});

此代码可能不太理想,因为它会修改 DS.Model 类,但是我没有看到一种扩展适配器或序列化器类来实现类似结果的方法。


Ember data expects my server to return the full object after each successful POST. However, my API only returns a kind of meta-object that contains the id. When Ember receives this object, the existing data in the record gets deleted except for the id.

So for example, when I do this:

var asset = App.Asset.store.createRecord('asset',{name: 'foo.ai',
    paths: ['/path/foo.ai', '/another/path/foo.ai'],
    type: 'ai',
    hash: '1234567890123456789012345678901234567890',
    datecreated: 'Sun, 12 Jan 2014 06:01:48 GMT',
    datemodified: 'Sun, 12 Jan 2014 06:01:48 GMT',
    thumbnail: '/path/to/thumb.jpg'
});

asset.save();

... the server responds, like so:

{
    status: "OK",
    updated: "Mon, 13 Jan 2014 19:49:14 GMT",
    id: "52d4433a8e3b08444740ea47",
}

When Ember receives this, it updates the id of the record with what it received in the server response, but it also deletes all the other data because it wasn't in the response. If I examine my data in the Ember helper, it looks like this:

{   
    id: '52d4433a8e3b08444740ea47'
    name: undefined,
    paths: undefined,
    type: undefined,
    hash: undefined,
    datecreated: undefined,
    datemodified: undefined,
    thumbnail: undefined
}

How can I override this behavior and get Ember to stop deleting my data?

I've looked at:

  • Hooking into createRecord in the DS.RESTAdapter, but it returns a promise, so I can't extract the new id yet.
  • Hooking into extractSave in the DS.RESTSerializer, but it expects the store, type, and payload as inputs, not the individual record! If the record were passed into here, I could update its id with what's in the payload and solve my problem.

Is there something that I'm missing?

解决方案

Looks like the existing data on the record is getting overwritten in the record's adapterDidCommit method.

See https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/model.js#L656-L657

One solution could be to extend that method to preserve existing data from the record. I believe it might look something like this.

DS.Model.reopen({
  adapterDidCommit: function() {
    var oldData = this.toJSON();
    this._super.apply(this, arguments); // calls the original adapterDidCommit method
    var newData = this._data;
    this._data = Ember.merge(oldData, newData);
  }
});

This code is probably not ideal because it modifies the DS.Model class but I didn't see a way to extend an adapter or serializer class to achieve a similar result.

这篇关于如果POST响应只包含id,那么如何将数据保存到我的Ember存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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