我想为我的网站实现搜索功能。当用户将搜索项foobar
键入input
框并提交时,他将被重定向到http://mydomain.com/search?query=foobar
。Backbone.js应该从URL抓取GET参数吗?
问题::应该怎么抢我从URL GET参数query
,并将其发送给后端,并得到结果的排列回为一个JSON响应?我应该这样做吗?
我目前的尝试下面甚至不会导致search
函数被触发。
路由器
var AppRouter = Backbone.Router.extend({
routes: {
'search?query=:query': 'search'
// ... and some other routes
},
search: function(query) {
this.photoList = new SearchCollection();
var self = this;
this.photoList.fetch({
data: {query: query},
success: function() {
self.photoListView = new PhotoListView({ collection: self.photoList });
self.photoListView.render();
}
});
}
});
var app = new AppRouter();
Backbone.history.start({
pushState: true,
root: '/'
});
用户可以去'domain.com','domain.com/something','domain.com/search'和路由器运行正确的函数来渲染元素在页面上。与页面上的链接/按钮/选项卡交互将'app.navigate()'到不同的URI段。这是否回答了重定向到index.html以支持'pushState'的问题? – Nyxynyx 2012-08-09 05:07:06
不,不。我想知道你是如何为pushState设置服务器的,它不是自动的。您特别需要配置您的Web服务器来处理其他404的重定向到index.html。 – tbranyen 2012-08-12 16:11:44
我在我的PHP框架中有一个路由器,用于在渲染包含backbone.js的页面之前检查路由是否有效。如果URL无效,那么PHP路由器将重定向到404。 – Nyxynyx 2012-08-12 16:43:34