Ember-Data: How do “mappings” work

前端 未结 3 1384
囚心锁ツ
囚心锁ツ 2020-12-08 11:52

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 em

相关标签:
3条回答
  • 2020-12-08 12:26

    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:

    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.

    0 讨论(0)
  • 2020-12-08 12:37

    The RESTAdapter expects the returned JSON to be of the form:

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

    The tests are a good source of documentation, see https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/rest_adapter_test.js#L315-329

    0 讨论(0)
  • 2020-12-08 12:44

    I'm using Ember Data rev. 11 and it seems that the plurals config in DS.RESTAdapter.create never works. I looked into the codes and found a solution as following:

    App.Adapter = DS.RESTAdapter.extend({
      bulkCommit: false
    })
    
    App.Adapter.configure('plurals', {
      series: 'series'
    })
    
    0 讨论(0)
提交回复
热议问题