2016-06-21 89 views
0

我一直有骨幹的問題,並問了幾個人的幫助,但沒有運氣到目前爲止。骨幹這是undefined

我創建一個新的集合這樣

let eventsCollection = new BandEvents({artist: 'Muse'}); 

在收集,我做這

const BandEvents = Collection.extend({ 
    model: Artist, 
    url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?api_version=2.0&app_id=SomeID', 

}); 

當我運行這段代碼,我得到以下錯誤在瀏覽器控制檯

Uncaught TypeError: Cannot read property 'artist' of undefined 

所以這在+ this.artist +是未定義的。我不知道我在做什麼錯。

回答

0

這是我如何解決它

我改變

let eventsCollection = new BandEvents({artist: 'Muse'}); 

let eventsCollection = new BandEvents($('#artist-input').val()); 

(我需要從輸入字段中獲取藝術家姓名)

並更改了

const BandEvents = Collection.extend({ 
    model: Artist, 
    url: 'http://api.bandsintown.com/artists/' + this.artist + '/events.json?api_version=2.0&app_id=SomeID', 

}); 

const BandEvents = Collection.extend({ 
    model: Artist, 
    initialize: function (artist) { 
     this.url = `http://api.bandsintown.com/artists/${artist}/events.json?api_version=2.0&app_id=ShowEvents`; 
    } 
}); 

爲此,我使用ES6的Template Literals