主干:从包含模型集合的模型集合中创建模型



我有一个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模型(题外话:将一个模型命名为"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;
  }
});

VenueEventsJsonVenue对象按预期创建,除了response.properties.events__all对象似乎没有进入JsonVenue模型(这是我希望使用它来创建EventSet集合的地方)。我在JsonVenue对象的initialize参数中放置了一个console.log(attributes),它显示,虽然JsonVenuefeatures.properties中的所有其他值都进入了模型,但events__all没有。

有什么原因会发生这种情况吗?这种将嵌套JSON数据加载到模型中的方法是否可行?在大多数示例中,人们只在JSON输出中包含嵌套对象的id,然后(我假设)在另一个JSON字符串中构建该对象的模型,并基于ID将它们关联起来。这似乎需要更多的服务器和客户端之间的流量。我也看到人们侧加载数据,这是在单个API调用中关联模型的推荐方法吗?

谢谢!

嗯…我刚刚尝试了你的代码,使用:

new VenueEvents(json, {parse: true});

创建您的集合。和…它工作得很好,看起来…

但是,骨干关系可能具有您希望简化代码的行为(这只是一个假设,我从未亲自测试过,也没有真正看过它)。

相关内容

  • 没有找到相关文章

最新更新