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

chciałbym zmienić skrót na stronie za pomocą następującego kodu:Zmień hash bez zdarzenie wyzwalające Sammy

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

Ale w tym przypadku Sammy wyzwala routing. Powiedz w kręgosłupie, że możemy to zrobić w ten sposób:

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

Czy można to zrobić w Sammy? Dzięki!

+0

Próbuję również dowiedzieć się, jak to zrobić, znalazłeś rozwiązanie @Andrew Luzkovky? – punkbit

Odpowiedz

0

użyć następującego kodu:

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

nie wiem native sposób to zrobić w Sammy, ale tutaj jest to rozwiązanie, które pracował dla mnie:

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

}); 

Następnie wywołaj funkcję quietRoute w kodzie:

//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"); 
Powiązane problemy