Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

block sammy.js from stealing 'real links'

I recently started using knockout.js and sammy.js to modernize my app. However I got stuck with some problems.

I have some valid links on the page - users should actually navigate to that location, instead of imitating navigation behaviors using sammy.js. I want only hash-based links to be routed by sammy.js, but it also intercepts links that does not contain any hashes.

for example, it intercepts <a href="/logout">logout</a>.

the js part that does routing is :

Sammy(function () {
    this.get('#/', function () {
         ...
    });

    this.get('#:id', function () {
         ...
    });

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

I think this.get('' .. ) part is the culprit that invokes this behavior - I got it from knockout.js tutorial, which says that the line is necessary to allow users from other origins to properly browse my web page. the page that is ran by knockout.js code is /w/. I want sammy.js to work only in /w/ or, at least allow users to navigate to /logout. How can I accomplish this?

like image 399
thkang Avatar asked Aug 31 '13 01:08

thkang


2 Answers

Been a while since I used Sammy, but I think you can turn off this behaviour with the disable_push_state setting:

Sammy(function() {
  this.disable_push_state = true;
  ...
});
like image 135
robertklep Avatar answered Nov 12 '22 16:11

robertklep


You can use "around" function. There is lot of options. I suggesting to do as follows,

Let as assume there is some urls. On hitting that url you want to communicate server to the page. For example, logout url like "/logout".

Now make that url as follows

"/logout?reload=true"

So that, you can control by following Sammy's code

Sammy(function(){

    this.around(function(callback) {
        if(this.params.reload === 'true')       
           location.replace(this.path);
        else
           callback();
    });
    // Your routers are going to be here

}).run()
like image 1
HILARUDEEN S ALLAUDEEN Avatar answered Nov 12 '22 17:11

HILARUDEEN S ALLAUDEEN