我有一个我正在使用 Trigger.io 构建的 Backbone 应用程序,当我单击手机工具栏中的后退按钮时,为什么数据会消失有点困惑。
这是设置:
我有一个视图 SearchResultsView,看起来像这样(为简洁起见,删除了 Trigger.io 代码):
window.SearchResultsView = Backbone.View.extend({
initialize: function() {
this.template = _.template($('#searchResultsView-template').html());
},
render: function() {
var self = this;
var renderedContent = this.template();
$(this.el).html(renderedContent);
if (window.resultsCollection) {
self.drawList(window.resultsCollection);
}
return this;
},
events: {
"click #submitQuery" : "processClick"
},
drawList: function(collection) {
collection.each(function(place){
window.console.log(place.get("name"));
$("#resultsList").append("<li><a href='#locationdetail/"+place.id+"' class='link'>" + place.get("name") +"</a></li>");
});
},
processClick: function() {
var self=this;
this.querystring = $("#searchBox").val(); //get the query
window.resultsCollection = new Places(null, {query: this.querystring});
window.resultsCollection.fetch(); //this is fetching via a call to the FourSquare API, and populating the models in the collection with that data
window.resultsCollection.bind('reset', function(collection){
self.drawList(collection);
});
}
});
这是模板:
<script type="text/template" id="searchResultsView-template">
<div id="searchbar"></div>
<h1>Results Bitch</h1>
<input type="text" id="searchBox"></input>
<input type="submit" id="submitQuery"></input>
<ul data-role="listview" id="resultsList">
</ul>
<a href="locationdetail">Click me</a>
</script>
这是路由器:
searchresults: function() {
this.searchresults = new SearchResultsView({});
$('#container').empty();
$('#container').append(this.searchresults.render().el);
},
以下是它在实践中的工作方式:当我最初加载页面时,没有结果列表(应该是这样)。我输入查询并执行搜索,它会返回并按应有的方式填充列表。然后,我将单击一个项目以转到另一个视图,当我单击“返回”时,列表再次为空。
我正在尝试做的是测试该集合是否仍然存在,如果存在,请继续并重新渲染列表,但它永远不会渲染列表。该集合确实存在,因为我可以将它记录到控制台并查看数据。
我怀疑#resultsList 元素尚不可用,因此当它将结果附加到它时,它找不到它,因此没有显示任何内容。
所以:
- 这听起来对吗?
- 为什么?当一切正常时,这与初始负载有何不同?
希望我足够清楚。为了简短起见,我没有包括模型和集合。如果这些有帮助,我很乐意将它们包括在内(尽管,我倾向于认为这是一个观点问题)。
谢谢!