11

我目前正在尝试将一些东西与 ember + emberdata + router + asp.net web api 放在一起。大多数似乎都有效,但是当 ember-data 尝试findAll通过我的模型的适配器时,我遇到了一条错误消息。

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

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

在我的应用程序中,我使用 ember-data 像这样表示它:

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

我还在我的应用程序中使用 RESTAdapter 定义了一个商店,如下所示:

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

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

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

路由器定义为

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');
                }
            })
        }),

        //...
    })
})

当我运行我的应用程序时,对于具有相同结构的每个对象,我都会收到以下消息:

未捕获的错误:断言失败:您的服务器返回密钥为 0 的哈希,但您没有映射

作为参考,这里是服务返回的 json:

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

我无法确切说出问题所在,并且由于断言提到我需要映射,所以我想知道:

  1. 这个映射什么以及如何使用它。
  2. 由于返回的 json 是一个数组,我应该在我的应用程序中使用不同类型的控制器,还是在 ember-data 中使用这种类型的 json 时我应该知道什么?还是应该更改服务器中的 JsonFormatter 选项?

欢迎任何帮助。

如果您觉得这不足以理解问题,我绝对可以添加更多信息。

编辑:我在后端更改了一些东西,现在我findAll()在服务器中的等效操作将输出序列化为以下 json:

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

但是我仍然无法让它在客户端中填充我的模型,并且我的错误消息已更改为:

未捕获的错误:断言失败:您的服务器返回了包含关键类型的哈希,但您没有映射

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

抛出此异常的方法是sideload并检查如下映射:

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(prop);返回undefined,然后我收到错误消息。该方法检查返回空typeForAssociation()的 for键。'associationsByName'Ember.Map

目前仍然没有解决方案。

顺便一提...

我的动作现在是这样的:

    // 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序列化的原始实现,因为我找不到配置选项来生成 Ember-Data 期望的 json 输出(如中所示{resource_name : [json, json,...]})。这样做的副作用是我失去了内置的 OData 支持,但我想保留它。有谁知道我如何配置它来为一个集合生成不同的 json?

4

3 回答 3

12

映射可以在 DS.RESTAdapter 中定义。我认为您可以尝试定义如下内容:

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
});

希望这可以解决您的问题。

更新:

目前,这没有很好的文档记录,我不记得我们是否通过阅读代码发现了它,或者可能是 Tom Dale 指出了它。
无论如何,这里是复数的重点 对于映射,我认为我们是由与您相同的错误驱动的,要么我们尝试过,要么汤姆教我们这个。

于 2012-08-30T22:09:18.930 回答
8

RESTAdapter期望返回的JSON形式为:

{
  "genres": [{
    "id": 1,
    "name": "action"
  },{
    "id": 2,
    "name": "Drama"
  }]
}

测试是很好的文档来源,请参阅https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/rest_adapter_test.js#L315-329

于 2012-08-29T18:54:50.280 回答
3

我正在使用 Ember Data rev. 11,似乎其中的plurals配置DS.RESTAdapter.create永远不会起作用。我查看了代码并找到了以下解决方案:

App.Adapter = DS.RESTAdapter.extend({
  bulkCommit: false
})

App.Adapter.configure('plurals', {
  series: 'series'
})
于 2013-03-28T09:10:35.087 回答