0

各会場で発生している多数の会場とイベントの GeoJSON データを生成する API があります。

出力例を参照してください。

{
   "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していないように見えることを除いて (コレクションを作成するために使用する予定です)。オブジェクトのパラメーターに a を入れたところ、 a内の他のすべての値がモデルに到達する一方で、はそうではないことがわかります。JsonVenueEventSetconsole.log(attributes)initializeJsonVenuefeatures.propertiesJsonVenueevents__all

これが起こる理由はありますか?ネストされた JSON データをモデルにロードするこの方法は可能ですか? ほとんどの例でidは、ネストされたオブジェクトの のみを JSON 出力に含め、(私が推測するに) 別の JSON 文字列でそのオブジェクトからモデルを構築し、ID に基づいてそれらを関連付けています。これには、サーバーとクライアントの間でより多くのトラフィックが必要になるようです。データをサイドローディングしている人もいますが、これは単一の API 呼び出しでモデルを関連付ける推奨される方法ですか?

ありがとう!

4

1 に答える 1