2014-10-01 108 views
0

我正在這個代碼,骨幹遺漏的類型錯誤

<script> 
    autocompleteRemote = new Backbone.AutocompleteList({ 
    url: function() { return 'http://ws.audioscrobbler.com/2.0/?method=artist.search&api_key=cef6d600c717ecadfbe965380c9bac8b&format=json&' + $.param({ artist: $('form#autocomplete-remote input[name=search]').val() }); }, 
    filter: null, 
    el: $('form#autocomplete-remote input[name=search]'), 
    template: _.template('<p><%= name.replace(new RegExp("(" + $("form#autocomplete-remote input[name=search]").val() + ")", "i") ,"<b>$1</b>") %></p>'), 
    delay: 500, 
    minLength: 3, 
    value: function(model) { return model.get('name') }, 
    }).resultsView.collection.parse = function(resp) { 
    return resp.results.artistmatches.artist; 
    }; 
</script> 

但我試圖將它連接到TMDB API這樣,

autocompleteRemote = new Backbone.AutocompleteList({ 
    url: function() { 
    return 'http://api.themoviedb.org/3/search/movie?api_key=' + api + '&' + $.param({query: $('form#autocomplete-remote input[name=search]').val()}) 
    }, 

    filter: null, 

    el: $('form#autocomplete-remote input[name=search]'), 
    template: _.template(
    '<p><%= name.replace(new RegExp("(" + $("form#autocomplete-remote input[name=search]").val() + ")", "i") ,"<b>$1</b>") %></p>' 
), 
    delay: 500, 
    minLength: 3, 
    value: function(model) { return model.get('name') } 
    , 

    }) 

    .resultsView.collection.parse = function(resp) { 
    return resp.results.moviematches.query; 
    }; 

    var api = 'a8f7039633f206xx42cd8a28d7cadad4' 

正如你可以看到我改了像url這樣的東西,並把api鍵放入var來清理代碼。我還將artist改爲query,這樣可以讓我找回正確的網址。但是在控制檯日誌中出現錯誤,我正在繪製一個白色。

Uncaught TypeError: Cannot read property 'query' of undefined 
Backbone.AutocompleteList.resultsView.collection.parse 
.extend.set 
options.success 
fire 
self.fireWith 
done 
callback 

源材料可以在這裏找到 - 使用>http://aghull.github.io/coding/2013/03/09/Backbone-autocomplete-lists/自動完成遠程採集

+0

錯誤提示resp.results.movi​​ematches未定義 - 您是否檢查過該響應以驗證它自己? – kinakuta 2014-10-01 09:17:42

+0

嗯,它看起來像'return resp.results.artistmatches.artist;'與audioscrobbler API有關。所以'moviematches'不是什麼。你可能有什麼建議我必須稱之爲從tmdb顯示電影? – 2014-10-01 09:35:03

+0

做一個console.dir(resp.results),以便檢查該響應並查看它包含您感興趣的屬性的位置。我對api一無所知,所以我沒有具體的答案您。 – kinakuta 2014-10-01 09:36:39

回答

1

Here是一個很好的資源,這將有助於找出響應體。從我在這裏產生的測試響應中可以看到,沒有moviematches屬性。你需要resp.results這只是一個對象(電影我猜)的集合(數組)。

所以,你需要更改您的代碼:

var api = 'a8f7039633f206xx42cd8a28d7cadad4'; 

autocompleteRemote = new Backbone.AutocompleteList({ 
    url: function() { 
    return 'http://api.themoviedb.org/3/search/movie?api_key=' + api + '&' + $.param({query: $('form#autocomplete-remote input[name=search]').val()}) 
    }, 

    filter: null, 

    el: $('form#autocomplete-remote input[name=search]'), 
    template: _.template(
    '<p><%= name.replace(new RegExp("(" + $("form#autocomplete-remote input[name=search]").val() + ")", "i") ,"<b>$1</b>") %></p>' 
), 
    delay: 500, 
    minLength: 3, 
    value: function(model) { return model.get('name') } 
    , 

    }).resultsView.collection.parse = function(resp) { 
    return resp.results; 
    }; 

我試圖做一個評論,但它成爲了一個答案:)

編輯:

使用此fiddle做檢查。放置正確的API_KEY並重試。我已嘗試使用您現有的api_key,但它無效。

+0

哼!當我編輯我的代碼時,我沒有收到任何錯誤,當我在Chrome瀏覽器中查看網絡概述時,我可以看到我提出的請求。但它不會以自動完成/建議的方式顯示結果。你有什麼建議嗎?無論如何非常感謝迄今。 – 2014-10-01 21:39:08

+0

我玩了一下,發現腳本正在工作(它在網絡概述中顯示了結果),但沒有在我的搜索中顯示。我檢查了實際的DOM,看到我得到一個空P元素列表。在模板中更改'name'並將值更改爲'original_title'(這是JSON中的電影名稱)後,它實際上可以工作:)。 – 2014-10-02 11:17:12

+0

我很高興它幫助:) – 2014-10-02 11:20:37