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

时间:2013-04-06 17:02:40

标签: backbone.js backbone-relational backbone-model

我有一个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的模型,然后每个Venues都包含一个名为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;
  }
});

VenueEventsJsonVenue个对象按预期创建,但response.properties.events__all对象似乎没有进入JsonVenue模型(是我希望用它来创建EventSet集合的地方。我在console.log(attributes)对象的initialize参数中添加了JsonVenue,它显示了features.properties的{​​{1}}内的所有其他值在模型方面,JsonVenue没有。

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

谢谢!

1 个答案:

答案 0 :(得分:1)

嗯..我刚试过你的代码,使用:

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

创建您的收藏。而且...... it works just fine it seems ......

尽管如此,Backbone-relational可能还有你希望简化代码的行为(这只是一个假设,我自己从未测试过,也没有真正看过它)。