2013-02-26 125 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