2012-09-27 61 views
1

我可以添加/使用sammy.js或任何其他可替代的插件這個如何添加在Sammy.js動態路由

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

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

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

回答

-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); 
    }); 
}); 
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) }); 
    }); 
}); 
2

首先選擇薩米應用

var app = Sammy('#main'); 

由於薩米按順序評估的路線,我們要確保新的路線被捕獲的任何之前添加的所有路由。

首先創建路線:

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

然後,它從陣列的底部移動到頂部:

app.routes.get.unshift(app.routes.get.pop());