灰烬数据映射

时间:2013-02-26 13:41:26

标签: javascript ember.js ember-data

我正在使用最新版本的ember-data(rev.11)和REST适配器从我的API中提取数据。返回的JSON示例如下所示:

{ 
    "events": [
        {
            "id": "5118dd8c4c80866ef2000051",
            "title": null,
            "starts_at": 1361901600,
            "ends_at": null,
            "currency": "SEK",
            "cents_door": 4000,
            "cents_advance": null,
            "price_door": "40.00 kr",
            "price_advance": null,
            "age_limit": null,
            "venue_section": "PLAYHOUSE",
            "description": null,
            "url": null,
            "repeats": null,
            "repeats_until": null,
            "venue_id": "nefertiti-jazz-club",
            "act_ids": [ "marias-playhouse" ]
        }
    ]
}

该模型如下所示:

App.Event = DS.Model.extend
  title: DS.attr('string')
  startsAt: DS.attr('number')
  endsAt: DS.attr('number')
  currency: DS.attr('string')
  centsDoor: DS.attr('number')
  centsAdvance: DS.attr('number')
  priceDoor: DS.attr('string')
  priceAdvance: DS.attr('string')
  ageLimit: DS.attr('string')
  venueSection: DS.attr('string')
  description: DS.attr('string')
  url: DS.attr('string')
  repeats: DS.attr('string')
  repeatsUntil: DS.attr('string')
  venue: DS.belongsTo('App.Venue')
  acts: DS.hasMany('App.Act')

但是在请求数据时,请求成功完成,但我在控制台中收到此错误:

  

未捕获错误:断言失败:您的服务器返回了哈希值   关键事件,但你没有映射

任何想法在这里出了什么问题?

===

更新:根据要求,我正在添加更多我的Ember.js应用程序。

我的RESTAdapter设置:

DS.RESTAdapter.registerTransform 'raw',
  deserialize: (serialized) ->
    serialized
  serialize: (deserialized) ->
    deserialized

App.Store = DS.Store.extend
  adapter: DS.RESTAdapter.create
    url: LJ.CONFIG.api.url
  revision: 11

路线:

App.Router.map ->
  this.resource 'events', ->
    this.route 'new'
  this.resource 'event', path: '/events/:event_id', ->
    this.route 'edit'
  this.resource 'venue', path: '/venues/:venue_id', ->
    this.route 'edit'
    this.resource 'events'
  this.resource 'act', path: '/acts/:act_id', ->
    this.route 'edit'
    this.resource 'events'
  this.route 'search', path: '/search/:term'
  this.route 'doc', path: '/docs/:doc'

2 个答案:

答案 0 :(得分:1)

乍一看,回应看起来很完美。

我的猜测是你发错了请求的格式错误。

此格式适用于多个events,即findAllfindQueryGET /events

但是,如果您为单个findGET /events/5118dd8c4c80866ef2000051

返回此回复,则可能会收到此错误

在这种情况下(当您只提取一个event时),您的回复应如下所示:

{
  "event": {
    "id": "5118dd8c4c80866ef2000051",
    "title": null,
    // ... rest of attributes
  }
}

答案 1 :(得分:1)

经过多次调试和搜索后,Ember.js似乎还不支持查询字符串参数。我不得不这样修改我的路线:

App.Router.map ->
  this.resource 'events', path: '/events/:country/:region/:city'
  this.route 'eventsNew', path: '/events/new'
  this.resource 'event', path: '/events/:event_id', ->
    this.route 'edit'
  this.resource 'venue', path: '/venues/:venue_id', ->
    this.route 'edit'
    this.resource 'events'
  this.resource 'act', path: '/acts/:act_id', ->
    this.route 'edit'
    this.resource 'events'
  this.route 'search', path: '/search/:term'
  this.route 'doc', path: '/docs/:doc'

这远非完美,但它现在有效。显然,查询字符串支持将在不久的将来发布。