烬-数据:如何"映射"工作 [英] Ember-Data: How do "mappings" work

查看:134
本文介绍了烬-数据:如何"映射"工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图与Ember + emberdata +路由器+ asp.net网站的API来把东西在一起。它的大部分似乎工作,但是我停留在一个错误消息,当灰烬数据通过适配器为我的模型试图的findAll 我得到的。

I'm currently trying to put something together with ember + emberdata + router + asp.net web api. Most of it seem to work, however I stuck in an error message I get when ember-data tries to findAll through the adapter for my models.

在我的后端我有这样(C#)的模型:

In my backend I have a model like this (C#):

public class Genre {
    [Key]
    public int Id { get; set; }
    [Required]
    [StringLength(50, MinimumLength=3)]
    public string Name { get; set; }
}

这在我的应用我重新present像这样使用烬数据:

Which in my app I represent it like this using ember-data:

App.Genre = DS.Model.extend({
    id: DS.attr("number"),
    name: DS.attr("string")
}).reopenClass({
    url: 'api/genre'
});

我也用我的应用程序中定义的商店RESTAdapter像这样:

I have also a Store defined in my App using the RESTAdapter like so:

App.store = DS.Store.create({
    revision: 4,
    adapter: DS.RESTAdapter.create({
        bulkCommit: false
    })
});

和商店在我的控制器中使用如下:

And the store is used in my controller as below:

App.GenreController = Ember.ArrayController.extend({
    content: App.store.findAll(App.Genre),
    selectedGenre: null
});

在路由器上定义为

The router is defined as

App.router = Em.Router.create({
    enableLogging: true,
    location: 'hash',
    root: Ember.Route.extend({
        //...

        genre: Em.Route.extend({
            route: '/genre',
            index: Ember.Route.extend({
                connectOutlets: function (router, context) {
                    router.get('applicationController').connectOutlet('genre');
                }
            })
        }),

        //...
    })
})

当我运行我的应用程序,我得到具有此相同的结构中的每个对象的以下信息:

When I run my application, I get the following message for every object that has this same structure:

未捕获的错误:断言失败:你的服务器返回一个散列
  0键,但你没有映射

Uncaught Error: assertion failed: Your server returned a hash with the key 0 but you have no mappings

有关参考,这里的服务是返回JSON:

For reference, here's the json the service is returning:

[
  {
    "id": 1,
    "name": "Action"
  },
  {
    "id": 2,
    "name": "Drama"
  },
  {
    "id": 3,
    "name": "Comedy"
  },
  {
    "id": 4,
    "name": "Romance"
  }
]

我不能告诉正是问题是什么,因为断言提的是,我需要映射,我想知道:

I cannot tell exactly what the problem is and since the assertion is mentioning that I need mapping, I'd like to know:


  1. 这是什么映射的的以及如何使用它。

  2. 由于返回的JSON是一个数组,我应该在我的应用程序使用不同类型的控制器,或者是有什么我应该知道,在灰烬数据这种类型的JSON的工作是什么时候?或者我应该改变服务器JsonFormatter选择?

  1. What this mapping is and how to use it.
  2. Since the returned json is an array, should I be using a different type of controller in my app ,or is there anything I should know about when working with this type of json in ember-data? or should I change the JsonFormatter options in the server?

任何帮助是值得欢迎的。

Any help is welcome.

我可以,如果你觉得这还不够了解的问题肯定添加更多信息。

I can definitely add more information if you feel this isn't enough to understand the problem.

修改:我在我的后端改变了一些东西,现在我的的findAll()服务器相当于动作序列化输出如下面的JSON:

EDIT: I've changed a few things in my backend and now my findAll() equivalent action in the server serializes the the output as the following json:

{
  "genres": [
      { "id": 1, "name": "Action" },
      { "id": 2, "name": "Drama" },
      { "id": 3, "name": "Comedy" },
      { "id": 4, "name": "Romance" }
   ]
}

但我仍然不能得到它来填充我的模型在客户端和我的错误信息已更改为这样的:

But I still can't get it to populate my models in the client and my error message has changed to this:

未捕获的错误:断言失败:你的服务器返回一个散列
  关键的流派,但你没有映射

Uncaught Error: assertion failed: Your server returned a hash with the key genres but you have no mappings

不知道还有什么我可能是做错了。

Not sure what else I might be doing wrong.

这是引发此异常的方法是<一个href=\"https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L236-262\"><$c$c>sideload而像这样的映射检查:

The method that throws this exception is sideload and checks for the mappings like this:

sideload: function (store, type, json, root) {
        var sideloadedType, mappings, loaded = {};

        loaded[root] = true;

        for (var prop in json) {
            if (!json.hasOwnProperty(prop)) { continue; }
            if (prop === root) { continue; }

            sideloadedType = type.typeForAssociation(prop);

            if (!sideloadedType) {
                mappings = get(this, 'mappings');
                Ember.assert("Your server returned a hash with the key " + prop + " but you have no mappings", !!mappings);
//...

这调用 sideloadedType = type.typeForAssociation(丙); 收益未定义,然后我得到的错误信息。该方法 typeForAssociation()的检查它返回一个空的 Ember公司'associationsByName 键.MAP

This call sideloadedType = type.typeForAssociation(prop); returns undefined and then I get the error message. The method typeForAssociation() checks for the for 'associationsByName' key which returns an empty Ember.Map.

仍然没有在目前这个解决方案。

Still no solution for this at the moment.

顺便说一句...

By the way...

我现在的动作是这样的:

My action is now like this:

    // GET api/genres
    public object GetGenres() {
        return new { genres = context.Genres.AsQueryable() };
    }

    // GET api/genres
    //[Queryable]
    //public IQueryable<Genre> GetGenres()
    //{
    //    return context.Genres.AsQueryable();
    //}

我不得不删除这得到由 json.NET 序列化的原始实现,因为我无法找到的配置选项,产生一个JSON输出灰烬,预计数据(如 {RESOURCE_NAME:[JSON,JSON,...]} )。这种副作用是,我已经失去了内置的OData的支持,但我想保留它。有谁知道我怎么能配置它产生不同的JSON的集合?

I had to remove the original implementation which gets serialized by json.NET as I could not find config options to produce a json output as Ember-Data expects ( as in {resource_name : [json, json,...]}). Side effect of this is that I've lost built-in OData support, but I'd like to keep it. Does anyone know how could I configure it to produce different json for a collection?

推荐答案

的映射可以在DS.RESTAdapter定义。我认为你可以尝试定义是这样的:

The mapping can be defined in the DS.RESTAdapter. I think you could try to define something like this:

App.Store = DS.Store.extend({
  adapter: DS.RESTAdapter.create({
    bulkCommit: true,
    mappings: {
      genres: App.Genre
    },
    // you can also define plurals, if there is a unregular plural
    // usually, RESTAdapter simply add a 's' for plurals.
    // for example at work we have to define something like this
    plurals: {
      business_process: 'business_processes' 
      //else it tries to fetch business_processs
    }
  }),
  revision: 4
});

希望这会解决您的问题。

Hope this resolves your problem.

更新:

Update:

在这个时候,这不是有据可查的,我不记得,如果我们发现它采用自行阅读code,或者是汤姆·戴尔指出就可以了。结果
无论如何,这里是<一个点href=\"https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L205\">plurals
对于映射,我觉得我们被同样的错误,你驱使,要么和我们尽力了,无论是汤姆TEACHED我们这一点。

At this time, this is not well documented, I don't remember if we found it by ourself reading the code, or perhaps Tom Dale pointed on it.
Anyway, here is the point for plurals For the mappings, I think we were driven by the same error as you, and either we tried, either Tom teached us about this.

这篇关于烬-数据:如何&QUOT;映射&QUOT;工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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