Ember.js Rest适配器:将JSON与无根映射(.NET Web API) [英] Ember.js Rest Adapter: mapping JSON with no root (.NET Web API)

查看:103
本文介绍了Ember.js Rest适配器:将JSON与无根映射(.NET Web API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,此服务以以下格式返回JSON:

  [
{id:1,name:John},
{id name:Jane}
]

然而,根据 Ember.js Rest适配器文档,Ember将期望JSON格式如下:

  {
persons:[
{id:1,name:John} ,
{id:2,name:Jane}
]
}

因此,Ember正在返回以下错误:
您的服务器使用键0返回散列,但您没有映射



我没有计划更改我的服务API以及如何返回数据。



可以让Ember.js(最新版本)与现有的一起使用我的服务返回的数据?而且,如果是这样,那么我该如何实现?

解决方案

在这个意义上,Ember非常灵活,适配器和序列化程序,以便将您的应用程序与任何后端API集成。



您应该获取WebAPIAdapter,它是 Web API的Ember.js模板



此外,你可能想看看这个项目,我写了一个例子,基于同一个模板(有一些修改我自己做的)。它仍在开发中,它并没有具有所有最佳实践(但是),但我会说这是一个有效的例子。



你还应该看看这个 repo / library (您可以安装它也可以通过NuGet ),它允许您将Handlebars模板预编译为 Ember.TEMPLATES 收集。


I have an existing service written with the .NET Web API.

As an example, this service returns JSON in the following format:

[
  { "id": 1, "name": "John" },
  { "id": 2, "name": "Jane" }
]

However, as per the Ember.js Rest Adapter documentation, Ember would expect JSON in the following format:

{
  "persons": [
    { "id": 1, "name": "John" },
    { "id": 2, "name": "Jane" }
  ]
}

Because of this, Ember is returning the following error: Your server returned a hash with the key 0 but you have no mapping for it

By no means do I plan on changing my service API and how it returns data.

Would it be possible to get Ember.js (latest version) to work with the existing data my service is returning? And, if so, how can I implement that?

解决方案

Ember is very flexible in that sense, giving the ability to extend the adapter and serializer in order to integrate your app with any backend API.

You should get the WebAPIAdapter, which is part of the Ember.js Template for Web API.

Additionally, you might wanna take a look into this project I wrote as an example, based on that same template (with some modifications I did on my own). It's still under development and it doesn't feature all the best practices (yet), but I'd say it's a valid example.

You should also take a look into this repo / library (You can install it via NuGet too), which allows you to pre-compile your Handlebars templates directly into Ember.TEMPLATES collection.

这篇关于Ember.js Rest适配器:将JSON与无根映射(.NET Web API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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