Javascript 余烬数据映射
我正在使用最新版本的ember data(rev.11)和REST适配器从API中提取数据。返回的JSON示例如下所示:Javascript 余烬数据映射,javascript,ember.js,ember-data,Javascript,Ember.js,Ember Data,我正在使用最新版本的ember data(rev.11)和REST适配器从API中提取数据。返回的JSON示例如下所示: { "events": [ { "id": "5118dd8c4c80866ef2000051", "title": null, "starts_at": 1361901600, "ends_at": null, "currency
{
"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')
{
"event": {
"id": "5118dd8c4c80866ef2000051",
"title": null,
// ... rest of attributes
}
}
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'
模型如下所示:
{
"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')
{
"event": {
"id": "5118dd8c4c80866ef2000051",
"title": null,
// ... rest of attributes
}
}
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'
但在请求数据时,请求成功完成,但我在控制台中遇到以下错误:
未捕获错误:断言失败:服务器返回的哈希值为
关键事件,但您没有它的映射
知道这里出了什么问题吗
===
更新:根据要求,我正在添加更多的Ember.js应用程序
我的重新适应程序设置:
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'
乍一看,反应似乎很完美 我的猜测是,您发送了错误请求的错误格式 此格式对许多
事件有效,这意味着findAll
或findQuery
(GET/events
)
但是,如果为单个find
(get/events/5118dd8c4c80866ef2000051)返回此响应,则可能会出现此错误
在这种情况下(当您仅获取一个事件时),您的响应应该如下所示:
{
"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')
{
"event": {
"id": "5118dd8c4c80866ef2000051",
"title": null,
// ... rest of attributes
}
}
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'
经过大量调试和搜索,Ember.js似乎还不支持查询字符串参数。我不得不像这样修改我的路线:
{
"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')
{
"event": {
"id": "5118dd8c4c80866ef2000051",
"title": null,
// ... rest of attributes
}
}
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'
这远非十全十美,但它目前仍在发挥作用。显然,查询字符串支持计划在不久的将来发布。这是正确的格式。在“events”键下返回多个事件,在“event”键下只返回一个事件。余烬和余烬数据可以很好地用于单个事件。它只是在这个有多个事件的路由上,它抛出了这个错误。返回上述响应的请求的URL是什么?它还没有公开,但URL看起来像:http://lj-web.dev/#/events/?location=goteborg-瑞典vastra gotaland county&origin=57.704123999999%2C11.9788316
。返回了正确的API响应,但ember数据似乎不喜欢它…响应格式与请求非常相关,这就是我询问请求的原因。。。您使用App.Event.find({..})
发出请求?我只是使用默认的余烬行为。我还没有为此编写任何特定的代码。我应该吗?沃特?你能纠正一下语法并进一步解释一下吗?这是否仍然相关?