1

我可以使用 sammy.js 或任何其他替代插件在某些点击事件上添加/定义动态路由吗

var app = $.sammy('#sidebar', function () {
  this.get("#/", function(){});
});

$(function(){
app.run("#/");

$("#btn").click(function(){
   app.addRoute ???????????
});
}
4

3 回答 3

2

首先选择 Sammy 应用

var app = Sammy('#main');

由于 Sammy 按顺序评估路线,我们需要确保在捕获所有路线之前添加新路线。

首先创建路线:

app.get('/Test/', function() { alert('new route') });

然后将它从数组的底部移动到顶部:

app.routes.get.unshift(app.routes.get.pop());
于 2014-02-07T20:54:28.153 回答
0

我假设您现在已经弄清楚了,但是这个问题应该有答案。您可以随时将路由添加到 Sammy 应用程序,就像在原始调用中使用“this”一样。

var app = $.sammy('#sidebar', function () {
    this.get("#/", function(){});
});

$(function(){
    app.run("#/");

    $("#btn").click(function(){
        // Add a new route to Sammy
        app.get('#/newroute/:id', function(context) { console.log(context) });
    });
});
于 2013-08-21T15:46:22.167 回答
-1
var app = $.sammy('#sidebar', function() {
    this.get("#/", function(){});
});

$(function() {
    app.run("#/");

    $("#btn").click(function() {
        //the callback can be a string to be parsed by the eval function, but is not recommended
        var callback = function() {
            //what you want to do the new route
        };

        //use the same get function from Sammy to add the new Route
        this.get('your new route', callback);
    });
});
于 2012-11-28T18:29:51.140 回答