2013-02-26 263 views
0

我正在使用最新版本的ember-data(修訂版11)和REST適配器從我的API中提取數據。返回的JSON的樣品看起來是這樣的:Ember數據映射

{ 
    "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') 

但請求數據時,請求成功完成,但我在控制檯中此錯誤:

Uncaught Error: assertion failed: Your server returned a hash with the key events but you have no mapping for it

任何想法這裏怎麼了?

===

更新:根據要求,我增加了一點我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' 

回答

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' 

這是遠遠不夠完美,但它現在的作品。顯然,查詢字符串支持將在不久的將來發布。

+0

掃管笏?你能否糾正語法並進一步解釋?這還是相關的嗎? – netpoetica 2013-05-01 17:33:49

1

響應看起來完美乍一看。

我的猜測是你發送的錯誤格式錯誤的請求。

此格式適用於許多events,這意味着findAllfindQueryGET /events

但是,如果你是返回單個findGET /events/5118dd8c4c80866ef2000051

這種反應,你可能會得到這個錯誤在這種情況下(當您只提取一個event時),您的回覆應該如下所示:

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

這是正確的格式。在「事件」鍵下返回多個事件,並且只有一個事件在「事件」鍵下返回。 Ember和餘燼數據適合單個事件。它只是在這條有多個事件的路線上,它拋出了這個錯誤。 – 2013-02-26 20:08:28

+0

返回上述響應的請求的URL是什麼? – 2013-02-26 20:44:59

+0

尚未公開,但網址如下:'http://lj-web.dev/#/events/?location = goteborg-vastra-gotaland-county-sweden&origin = 57.70412399999999%2C11.9788316'。正確的API響應被返回,但是ember-data似乎並不喜歡它...... – 2013-02-26 20:47:05