(SPA) Single page application using pushState with sammy.js

JavaScript
voted: 1
vote
spa pushstate routing html5

How you can mix pushState with sammy.js while writing single page application. Example is written on top of jQuery. Requesting page from your browser via URLs: localhost/ localhost/example localhost/example/test will not reload the page and it will log sammys callback in console.

var app = function () {
    var self = this;
    self.sammyUrls = [
     "/",
     "/example",
     "/example/:param1"
    ];
    self.run = function () {
        $(document).on("click", "a", function (e) {
            var $target = $(e.target), url = $target.prop("href");

            e.preventDefault();
            history.pushState({}, $target.prop("title"), url);
        });
        $.sammy(function () {
            var sammy = this;
            self.sammyUrls.forEach(function (url) {
                sammy.get(url, function (param) { console.info(url, param); });
            });
        }).run();
    }
};

var application = new app();
application.run();
working url: not entered
version: not entered
Leave a Comment:
Characters left:
No comments yet.