2015-09-25 91 views
0

我試圖填充來自JSON的Backbone集合,它將API返回給我。然而,在我嘗試填充集合後,我用console.log()獲取了這個: playlistSpotify child {length: 1, models: Array[1], _byId: Object}。但是我的集合應該包含3個對象(返回的JSON中有3個對象)。骨幹 - 從JSON收集不填充

發生了什麼事情的任何想法?

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的還給我(我volontary刪除一個項目,使其更短):

    { 
        "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 
    } 
    

經過一番摸索我看this事情時,我CONSOLE.LOG集合。我無法真正瞭解發生了什麼事。

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

回答

1

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

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

要初始化集合,你只需要簡單地傳遞對象作爲參數數組來collection

+0

constructor哦,該死的,我覺得這麼愚蠢!謝謝 ! –