gpt4 book ai didi

javascript - 在不触发 Sammy 事件的情况下更改哈希

转载 作者:可可西里 更新时间:2023-11-01 02:25:19 25 4
gpt4 key购买 nike

function UsersVM(start_page){
var self = this;
console.log('start form ' + start_page);
self.go_to = function(page) {
location.hash = '#Users/' + pageNumber;
}
}

Sammy(function() {
this.get('/app/?#Users/:page', function () {
var vm = new UsersVM(this.params.page);
ko.applyBinding(vm);
});
}).run();

我想用以下代码更改页面的哈希值:

    location.hash = '#Users/' + pageNumber;

但在这种情况下,Sammy 会触发路由。在 Backbone 中说我们可以这样做:

    app.navigate("help/troubleshooting", {trigger: false}); 

是否可以在 Sammy 中进行?谢谢!

最佳答案

我不知道在 Sammy 中有什么本地方法可以做到这一点,但这里有一个对我有用的解决方案:

var sam = $.sammy(function () {
var sammy = this; //get a persistent reference to this

sammy.quiet = false; //set quiet to false by default

//I set quiet to true before running a route
sammy.quietRoute = function (location) {
sammy.quiet = true;
sammy.setLocation(location);
}

//I'm called after every route to reset quiet to false
sammy.after(function () {
sammy.quiet = false;
});

//I'm a 'normal' route that does not have the capability to be 'quiet'
this.get('#normalRoute', function () {
//routing code
});

//I am a route that can be 'quieted' so that when the url or
//hash changes my routing code doesn't run
this.get('#quietableRoute', function () {
if (!sammy.quiet) {
//routing code
} else {
return;
}
});

});

然后在您的代码中调用 quietRoute 函数:

//This will work
sam.quietRoute("#quietableRoute");

//This will not work because the "if(!sammy.quiet)..." code has not been
//implemented on this route
sam.quietRoute("#normalRoute");

关于javascript - 在不触发 Sammy 事件的情况下更改哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com