2014-10-01 105 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