Search code examples
javascriptknockout.jssammy.js

Knockout.js and Sammy.js wrong behaviour


I'm developing admin panel to my website and decided to do it with knockout and sammy. But I am faced with a routing problem. I have two pages:

http://localhost/admin/element and http://localhost/admin/category. 

On my element page I have the following Sammy config:

Sammy(function() {
            this.get('#:currentpage', function() {
                self.reloadPage(this.params.currentpage);
            });

            this.get('', function() {
                this.app.runRoute('get', '#1');
            });
        }).run();

Everything works perfect but if I try to go to another page (by usual link, e.g. Edit Categories) I just get to the empty route on the same page, so I just cannot go to another page with link. Any ideas how to fix that?


Solution

  • Don't use '' in your Sammy configuration. Try '/' for root page or '/admin/element' for your elements instead.

    var Router = function() {
        var sammy = new Sammy.Application(function() {
    
                this.get('#:currentpage', function(context) {
                    alert(context.params.currentpage);
                });
    
                this.get('/admin/element', function () {
                    this.app.runRoute('get', '#1');
                });
    
            }),
            run = function() {
                sammy.run();
            };
    
        return {
            run: run
        };
    };
    
    $(function() {
        var r = new Router();
        r.run();
    });
    

    PS: The example uses version of Sammy 0.7.1. In version 0.6.3 there is another behavior.