2013-01-04 53 views
10
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; 

但在這種情況下,薩米觸發路由。在主幹說我們可以這樣做:

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

是否有可能在Sammy中做到這一點? 謝謝!

+0

我也想弄清楚如何做到這一點,您找到了解決方案@Andrew Luzkovky? – punkbit

回答

0

使用下面的代碼:

var new_location = '#foo'; 
app.trigger('redirect', {to: new_location}); 
app.last_location = ['get', new_location]; 
app.setLocation(new_location); 
5

我不知道土生土長方式薩米要做到這一點,但這裏是已爲我工作的解決方案:

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