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

サミーでもできるの?ありがとう!

4

2 に答える 2

7

サミーでこれを行うためのネイティブな方法はわかりませんが、私にとってうまくいった解決策は次のとおりです。

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 に答える