我可以使用sammy.js或任何其他替代插件为某个点击事件添加/定义动态路由
var app = $.sammy('#sidebar', function () {
this.get("#/", function(){});
});
$(function(){
app.run("#/");
$("#btn").click(function(){
app.addRoute ???????????
});
}
答案 0 :(得分:2)
首先选择Sammy app
var app = Sammy('#main');
由于Sammy按顺序评估路线,我们需要确保在捕获所有路线之前添加新路线。
首先创建路线:
app.get('/Test/', function() { alert('new route') });
然后将它从数组底部移到顶部:
app.routes.get.unshift(app.routes.get.pop());
答案 1 :(得分: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 :(得分:-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);
});
});