【发布时间】:2013-04-06 17:02:40
【问题描述】:
我有一个 API 可以生成多个地点的 GeoJSON 数据以及每个地点发生的事件。
查看示例输出:
{
"crs":null,
"type":"FeatureCollection",
"features":[
{
"geometry":{
"type":"Point",
"coordinates":[
-122.330056,
47.603828
]
},
"type":"Feature",
"id":39,
"properties":{
"city_slug":"seattle",
"neighborhood_name":"Downtown",
"events__all":[
{
"category__category":"Gallery",
"eventid":16200847,
"description":"A Wider View, curated by Onyx Fine Arts Collective, features 60 works by 23 artists of African descent.",
"title":"A Wider View",
"cost":"Free",
"category__slug":"gallery",
"slug":"a-wider-view"
}
],
"venue_name":"City Hall Lobby Gallery",
"venue_address":"600 4th Avenue, Seattle, WA 98104, USA",
"city_name":"Seattle",
"neighborhood_slug":"downtown",
"venue_slug":"city-hall-lobby-gallery"
}
},
{
"geometry":{
"type":"Point",
"coordinates":[
-122.348512,
47.6217233
]
},
"type":"Feature",
"id":42,
"properties":{
"city_slug":"seattle",
"neighborhood_name":"Downtown",
"events__all":[
{
"category__category":"Museums",
"eventid":15455000,
"description":"The Art of Video Games tackles a 40-year history, with a focus on video game as art form. Nerdy heartstrings will be tugged in this nostalgia-inducing retrospective, including everything from the Atari VCS to Playstation 3.",
"title":"The Art of Video Games",
"cost":"$20",
"category__slug":"museums",
"slug":"the-art-of-video-games"
},
{
"category__category":"Museums",
"eventid":15213972,
"description":"There's just something about the black leather jacket. It's a garment that invariably comes with context, that cannot help but be an icon. Worn to Be Wild: The Black Leather Jacket explores the evolution of the leather jacket from \"protective gear to revolutionary garb.\"",
"title":"Worn to Be Wild: The Black Leather Jacket",
"cost":"$20",
"category__slug":"museums",
"slug":"worn-to-be-wild-the-black-leather-jacket"
}
],
"venue_name":"Experience Music Project | Science Fiction Museum.",
"venue_address":"325 5th Avenue North, Seattle, WA 98109, USA",
"city_name":"Seattle",
"neighborhood_slug":"downtown",
"venue_slug":"experience-music-project-science-fiction-museum"
}
}
],
"bbox":[
-122.348512,
47.6035448,
-122.3233742,
47.6217233
]
}
我想将其映射到一个名为 VenueEvents 的集合中。 VenueEvents 包含名为 JsonVenues 的模型,然后这些场地中的每一个都包含一个名为 EventSet 的集合,其中包含许多 Event 模型(旁白:将模型命名为“事件”是灾难的根源吗?) .
我的模型概述如下:
var Event = Backbone.Model.extend({
parse: function(response){
return {
id: response.eventid,
slug: response.slug,
title: repsonse.title,
description: response.description,
category: response.category__category,
cost: response.cost
}
}
});
var EventSet = Backbone.Collection.extend({
model: Event,
}
});
var JsonVenue = Backbone.Model.extend({
initialize: function(attributes) {
console.log(attributes)
},
parse: function(response){
// var eventSet = new EventSet(response.properties.events__all);
return {
name: response.properties.venue_name,
address: response.properties.venue_address,
neighborhood: response.properties.neighborhood_name,
//eventSet: eventSet
}
}
});
// Is this actually a model?
var VenueEvents = Backbone.Collection.extend({
model: JsonVenue,
url: '/json/',
parse: function(response){
return response.features;
}
});
VenueEvents 和 JsonVenue 对象按预期创建,但 response.properties.events__all 对象似乎无法使用 JsonVenue 模型(这是我希望使用的模型)它来创建EventSet 集合)。我在JsonVenue 对象的initialize 参数中放置了一个console.log(attributes),它表明虽然JsonVenue 的features.properties 中的所有其他值都进入了模型,但events__all 确实如此不是。
有什么理由会发生这种情况吗?这种将嵌套的 JSON 数据加载到模型中的方法是否可行?在大多数示例中,人们仅在其 JSON 输出中包含嵌套对象的 id,然后(我假设)在另一个 JSON 字符串中从该对象构建模型并根据 ID 关联它们。这似乎需要服务器和客户端之间的更多流量。我也看到有人侧载数据,这是推荐的在单个 API 调用中关联模型的方法吗?
谢谢!
【问题讨论】:
-
您是否真的在使用 Backbone-relational(作为 Backbone 插件)?你把它放在标签里。如果您一次加载所有内容,这对您来说可能会很有趣。至于你的问题,没人回答你吗,我有时间再研究一下。
标签: backbone.js backbone-relational backbone-model