12
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 中可以做到吗?谢谢!

4

2 回答 2

7

我不知道在 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");
于 2013-09-13T13:15:35.530 回答
0

使用以下代码:

var new_location = '#foo';
app.trigger('redirect', {to: new_location});
app.last_location = ['get', new_location];
app.setLocation(new_location);
于 2013-05-24T03:53:16.053 回答