簡體   English   中英

骨干-集合未從JSON填充

[英]Backbone - Collection not populating from JSON

我正在嘗試從Spotify API返回我的JSON填充Backbone集合。 但是,在嘗試填充集合之后,我需要使用console.log(): playlistSpotify child {length: 1, models: Array[1], _byId: Object} 但是我的集合應包含3個對象(返回的JSON中的3個對象)。

有什么想法嗎?

JS:

  • 型號:

     module.exports = Backbone.Model.extend({ defaults: { id: null, selected : false, name: null }, initialize: function() { } }); 
  • 集合:

     module.exports = Backbone.Collection.extend({ model : Playlist, initialize: function() { } }); 
  • 查看(僅是加載JSON的函數):

     loadSpotifyPlaylists : function() { var that = this; $.ajax({ url: 'https://api.spotify.com/v1/users/'+ this.user.get('spotifyId') +'/playlists', headers: { 'Authorization': 'Bearer ' + this.user.get('spotifyToken') }, success: function(response) { var playlistCollection = new Playlists2({ collection : JSON.stringify(response.items) }); var playlistView = new PlaylistSpotifyView({ collection : playlistCollection }); that.$playListsSpotify.append(playlistView.render().el); } }); }, 
  • JSON Spotify返回我(我自願刪除了一項以使其更短):

     { "href": "https://api.spotify.com/v1/users/loco/playlists?offset=0&limit=20", "items": [ { "collaborative": false, "external_urls": { "spotify": "http://open.spotify.com/user/loco/playlist/6MpEay73SWJzyJHGu5u6bK" }, "href": "https://api.spotify.com/v1/users/loco/playlists/6MpEay73SWJzyJHGu5u6bK", "id": "6MpEay73SWJzyJHGu5u6bK", "images": [ { "height": 640, "url": "https://mosaic.scdn.co/640/dc8743ffb149138cfe29334147b835532dbee0ddf3320826…dc7e917657c7982eab6ed5126307c6c3a67e1a3fb78245a8477312eeaec1621a2849969219", "width": 640 }, { "height": 300, "url": "https://mosaic.scdn.co/300/dc8743ffb149138cfe29334147b835532dbee0ddf3320826…dc7e917657c7982eab6ed5126307c6c3a67e1a3fb78245a8477312eeaec1621a2849969219", "width": 300 }, { "height": 60, "url": "https://mosaic.scdn.co/60/dc8743ffb149138cfe29334147b835532dbee0ddf33208261…dc7e917657c7982eab6ed5126307c6c3a67e1a3fb78245a8477312eeaec1621a2849969219", "width": 60 } ], "name": "quizz musical", "owner": { "external_urls": { "spotify": "http://open.spotify.com/user/loco" }, "href": "https://api.spotify.com/v1/users/loco", "id": "loco", "type": "user", "uri": "spotify:user:loco" }, "public": false, "snapshot_id": "1OUgCAhN+ZsJo6whDez1kVA/R2DooVY4Rzw+Vij5HYHgz/PDFpjbUaiXz+fkapX7", "tracks": { "href": "https://api.spotify.com/v1/users/loco/playlists/6MpEay73SWJzyJHGu5u6bK/tracks", "total": 64 }, "type": "playlist", "uri": "spotify:user:loco:playlist:6MpEay73SWJzyJHGu5u6bK" }, { "collaborative": false, "external_urls": { "spotify": "http://open.spotify.com/user/loco/playlist/2KlAANyACpjJZmZfVGK0Mb" }, "href": "https://api.spotify.com/v1/users/loco/playlists/2KlAANyACpjJZmZfVGK0Mb", "id": "2KlAANyACpjJZmZfVGK0Mb", "images": [ { "height": 640, "url": "https://i.scdn.co/image/24e6e9aac4ea49d92e260bb6875f4882c65c7f48", "width": 640 } ], "name": "Playlist2", "owner": { "external_urls": { "spotify": "http://open.spotify.com/user/loco" }, "href": "https://api.spotify.com/v1/users/loco", "id": "loco", "type": "user", "uri": "spotify:user:loco" }, "public": true, "snapshot_id": "fqLltawhg+mMNV+nVEl5Rmj94uDI1kdbbzoZLPbs7uVtZclbYJqyEtIAvIacExVe", "tracks": { "href": "https://api.spotify.com/v1/users/loco/playlists/2KlAANyACpjJZmZfVGK0Mb/tracks", "total": 1 }, "type": "playlist", "uri": "spotify:user:loco:playlist:2KlAANyACpjJZmZfVGK0Mb" } ], "limit": 20, "next": null, "offset": 0, "previous": null, "total": 3 } 

經過一番探索之后,當我用console.log收集集合時,我看到了這個東西。 我真的不明白發生了什么。

任何幫助,將不勝感激 ! :)

您創建了錯誤的實例。 將其更改為下一個:

var playlistCollection = new Playlists2(response.items);
var playlistView = new PlaylistSpotifyView({ model : playlistCollection });

要初始化集合,您只需要簡單地將對象數組作為參數傳遞給collectionconstructor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM