开发者

Sammy.JS without automatic runRoute on app.run?

开发者 https://www.devze.com 2023-02-28 09:15 出处:网络
Is there a way to make it so Sammy.JS does not automatically cal开发者_StackOverflow社区l runRoute when you call app.run()?

Is there a way to make it so Sammy.JS does not automatically cal开发者_StackOverflow社区l runRoute when you call app.run()?

My code currently initializes Sammy on the first load, but does not want it to actually call any sammy routes until the user actually clicks a link.


You could try passing in a non-operational route to the run method. It might ignore any route in the hash in that case.

Otherwise, you could set a listener on the document root to listen for clicks in the document and run the application then. But this solution seems "less clean."

(assuming jQuery)

$(function () {
    var app = Sammy();

    $("a").live(function () {
       if (!app.isRunning()) {
          app.run();
       }
    });
});


Question is old but I used the following solution that I find a little cleaner.

The page is loaded normally and Sammy doesn't call the current route when using .run().

http://sammyjs.org/docs/api/0.7.4/Sammy.Application.methods.before

var appIsRunning = false;

var app = Sammy(function() {

   this.before('.*', function() {

        if (this.path == document.location.pathname && appIsRunning == false) {
            return false;
        }
   });

   // The routes...
   ...

}

// start the application
app.run();
if (app.isRunning()) { appIsRunning = true; }
0

精彩评论

暂无评论...
验证码 换一张
取 消