2013-04-06 172 views
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 
    ] 
} 

我想這個映射到一個集合稱爲VenueEventsVenueEvents包含模型稱爲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; 
    } 
}); 

獲得創建VenueEventsJsonVenue對象如預期,不同之處在於該response.properties.events__all對象似乎並沒有使它的方式向JsonVenue模型(這是我期望用它來創建EventSet集合)。我已將console.log(attributes)放入JsonVenue對象的initialize參數中,並且它顯示的features.properties中的所有其他值都通向該模型,但events__all則沒有。

是否有任何理由爲什麼這會發生?這種將嵌套JSON數據加載到模型中的方法甚至可能嗎?在大多數示例中,人們只在他們的JSON輸出中包含嵌套對象的id,然後(我假設)在另一個JSON字符串中將該對象從該對象中構建出來並根據ID關聯它們。這似乎需要服務器和客戶端之間的更多流量。我還看到人們側面加載數據,這是在單個API調用中關聯模型的推薦方法嗎?

謝謝!

+0

你實際使用的骨幹關係(作爲骨幹插件)?你把它放在標籤中。如果你一次加載所有內容,這可能對你很有意思。至於你的問題,沒有人會回答你,我會在有空的時候看看它。 – Loamhoof 2013-04-06 18:35:48

回答

1

嗯..我剛試過你的代碼,使用:

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

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

但是,Backbone-relational可能要簡化你的代碼的行爲(這只是一個假設,我從來沒有測試它自己,也有它真實的樣子)。

+0

你完全正確。顯然它按預期工作。這讓我意識到,我收到的JSON流實際上並沒有EventSet(需要通過get請求傳入參數)。感謝您解決問題的幫助! – alukach 2013-04-07 17:33:01