当调用ember数据的destroyRecord()时,如何从json-api服务器的200响应中访问顶级元键 [英] How to access top level meta key from json-api server's 200 response when calling ember data's destroyRecord()

查看:132
本文介绍了当调用ember数据的destroyRecord()时,如何从json-api服务器的200响应中访问顶级元键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



根据json-api规范(),我正在使用一个Ember应用程序,该应用程序正在使用Ember Data和现在的json-api适配器。 href =http://jsonapi.org/format/#crud-deleting =nofollow> http://jsonapi.org/format/#crud-deleting )删除记录时,您的服务器应该如果删除成功,则返回 200 响应,服务器只回复顶级 meta 键。



我目前的服务器只是这样做,我试图找出当使用Ember Data的 model.destroyRecord时如何访问顶级元对象中的数据()方法

  myModel.destroyRecord() $ b //返回的值是模型,如何获取服务器返回的实际元数据
//
});

服务器响应包含有关删除的内容的信息,如下所示:

  {
meta:{
num-deleted-a:10,
num-deleted-b: 100,
num-deleted-c:200
}
}

我想获取这些信息,以便我们可以将它显示给用户。



谢谢!



我正在使用以下版本:

  Ember:2.2.0 
Ember数据:2.3.3
jQuery:1.11.3


解决方案

Ember不支持元元 code> destroyRecord )



如果你想要这个,你必须挂钩到ember内部。



以下代码使用ember 2.3中的ember内部版本,可能会在将来的版本中破坏 / p>

商店上有未记录的 _metadataFor 功能给出给定类型的最后一个元数据。我使用一个自定义的初始化器来保存它到模型

 从ember导入Ember; 
从ember-data导入DS;
const {set} = Ember;
export函数initialize(application){
DS.Model.reopen({
meta:null,
didCommit(){
this._super(... arguments );
set(this,'meta',this.store._metadataFor(this.constructor.modelName));
}
});
};

export default {
name:'meta',
initialize:initialize
};

此后,您可以执行 model.save()。then(() => console.log(model.get('meta'))) model.destroyRecord.then(()=> console.log(model.get 'meta')))



也许结帐这个ember-twiddle。


I'm working on an Ember app that is using Ember Data and the now default json-api adapter.

According to the json-api spec (http://jsonapi.org/format/#crud-deleting) when deleting a record your server should return a 200 response if the deletion is successful and the server responds with just a top level meta key.

My current server does just this and I'm trying to figure out how to access the data in the top level meta object when using Ember Data's model.destroyRecord() method.

myModel.destroyRecord().then(function(model){
    // the returned value is the model.  How can I get the actual metadata 
    // returned by the server?   
});

The server response contains information about what exactly was deleted and looks like this:

{
   "meta": {
      num-deleted-a: 10,
      num-deleted-b: 100,
      num-deleted-c: 200
    }
}

I'd like to get this information so I can display it to the user.

Thank you!

I am using the following versions:

Ember             : 2.2.0
Ember Data        : 2.3.3
jQuery            : 1.11.3

解决方案

Ember does not support meta for single model requests (find,save and destroyRecord) at the moment!

If you want this you have to hook into ember internals.

The following code uses ember internals from ember 2.3 and may break in future versions!

There is the undocumented _metadataFor function on the store that gives you the last metadata for a given type. I use a custom initializer to always save it to the Model:

import Ember from 'ember'; 
import DS from 'ember-data';
const {set} = Ember;
export function initialize(application) {
  DS.Model.reopen({
    meta: null,
    didCommit() {
      this._super(...arguments);
      set(this, 'meta', this.store._metadataFor(this.constructor.modelName));
    }
  });
};

export default {
  name: 'meta',
  initialize: initialize
};

After this you can do model.save().then(() => console.log(model.get('meta'))) or model.destroyRecord.then(() => console.log(model.get('meta'))).

Maybe checkout this ember-twiddle.

这篇关于当调用ember数据的destroyRecord()时,如何从json-api服务器的200响应中访问顶级元键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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