2013-01-04 14 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(); 

Tôi muốn thay đổi băm của trang với đoạn mã sau:Thay đổi băm mà không cần kích hoạt Sammy kiện

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

Nhưng trong trường hợp này Sammy gây nên định tuyến. Nói trong Backbone, chúng ta có thể làm theo cách này:

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

Có thể làm điều đó ở Sammy không? Cảm ơn!

+0

Tôi cũng đang tìm cách thực hiện, bạn đã tìm thấy giải pháp @Andrew Luzkovky? – punkbit

Trả lời

0

Sử dụng đoạn mã sau:

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

Tôi không biết một cách tự nhiên để làm điều này trong Sammy, nhưng đây là một giải pháp mà đã làm việc cho tôi:

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

}); 

Sau đó, gọi hàm quietRoute trong mã của bạn:

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