1

这对某些人来说似乎微不足道,但我有一个应用程序可以执行搜索并使用包含搜索词的哈希更新浏览器历史记录。

routes: {
"": "search",
"search/:query": "searchSetup"
},

根据我对主题标签的了解,有效的主题标签不应包含空格。当用户搜索例如视频游戏时,每个人都如何处理这个问题。似乎当我搜索上面的术语时,导航事件被触发了两次我的正则表达式目前很糟糕。

    RT.App.searchRouter = Backbone.Router.extend({
    initialize: function() {
        RT.App.vent.on("change:hash", function(searchTerm) {
            console.log("chnaging hash")
            this.navigate('search/'+searchTerm);
        }, this);
    },
    routes: {
        "": "search",
        "search/:query": "searchSetup"
    },
    createView: function () {
        //has view already been instatiated?
        if(!$("#forms-demo").length) {
            new RT.App.views.AdvancedSearchView({
                el: '#ui-search',
                model: new RT.App.models.AdvancedSearch()
            });

        }   

    },
    search: function() {
        //clear any previous search if any
        this.createView();
    },
    searchSetup: function(query) {
        this.createView();
        var $searchinputEl = $("#search-term"),     

        //check if storage entry exists and if it matches the query entered
            storedSearchTerm = amplify.store('search');
        if((!_.isUndefined(storedSearchTerm) && !_.isUndefined(query)) && (storedSearchTerm.term === unescape(query))) {

            //retrieve storage and perform search based on preferences
            RT.App.searchCategoryOptions.attributes = storedSearchTerm.sections;

            //each filter view options should now set the correct options
            RT.App.vent.trigger("change:options");

            //add search query to search input
            $searchinputEl.val(storedSearchTerm.term);

            //trigger search

            $(".trigger-search").trigger("click");
        }
    }
});
$(function(){

    // Instantiate the router
    var searchRouter = new RT.App.searchRouter();
    if(Backbone.History.started) {
        Backbone.history.stop();
    }
    Backbone.history.start();
});
4

1 回答 1

0

您可以处理空格。只要确保首先对您的搜索词进行编码。

this.navigate('search/'+ encodeURIComponent(searchTerm));
于 2013-05-22T14:34:17.660 回答