javascriptknockout.jsurl-routingsammy.js

How to set up input value in hash?


 self.search = function() {
        searchRes = $('#search_field').val();
        location.hash = "search" + "/" + searchRes;
    };     
 Sammy(function () {
                    this.get('#search',':searchRes', function() {
                    this.param.searchRes = $('#search_field').val();
                        $('.inside').each(function() {
                            console.log(this.params);
                            if ($(this).text().search($('#search_field').val()) == -1) {
                                $(this).parent().addClass('blind');
                            }
                        });
                    });
                }).run();

I set up input value in the url, but I can't get the hash, which I need, to pass through the get function. I get an error like this:

/#search/resultError {message: "404 Not Found get /#search/result"}


Solution

  • Accordingly to the SammyJS docs your route have to be defined as follow :

    this.get('#/search/:searchRes', function() { 
        var searchRes= this.params['searchRes'];    
    });
    

    As you can fetch the search criteria from the param property I suggest to use this sample :

    var searchRes = this.params['searchRes']; 
    

    The SammyJS object provides an redirect method.

    self.search = function() {
        searchRes = $('#search_field').val();
        app.redirect("#/search" + "/" + searchRes;);          
    };   
    

    I hope it helps.