2012-02-13 16 views

cevap

14

Bunu yapmak için resmi olarak desteklenen bir yol yok (bildiğim). Eğer herhangi yönlendirici devre dışı bırakmak istiyorsanız, belgesiz, ancak bu yorum ile kaynak kodunda gösterir Backbone.history.stop();, kullanabilirsiniz: Bazı passthrough durumunu kodlamak için Aksi

// Disable Backbone.history, perhaps temporarily. Not useful in a real app, 
// but possibly useful for unit testing Routers. 

, sen olurdu senin yönlendiricinin rota işleyicileri yönlendirici devlet ise "özürlü" ya da öyle bir şey. Ya da kayıt dışı Backbone.history.handlers (- bir regexp'nin olarak - .route içeren iç dizi ve .callback) yineleme ve bu özel yönlendirici ilişkin yolları çıkarın. Belli ki, belgesiz olmamaları ve hepsi, bu, Backbone'un gelecekteki sürümlerinde değişime açıktır.

+0

komik) backbone.js, ben App yararlıdır dışına giriş yaptıktan sonra devre dışı bırakmayı düşünürdüm, i idare etmenin başka yolları da vardır t, ama backbone.history.stop gibi gerçekten olur – pushplaybang

0

Yönlendiricinize örneğinin kontrol edebiliyoruz eğer, aşağıdakileri yapabilirsiniz:

var myRouter = new MyRouter({ routes: function(){ 
    return; 
}}); 
0

Sen (o olmayan bir API yöntemini kullanır kesmek tabanlı çözüm kullanabilir ve yeni sürümleri ile çalışmayı durdurabilir

var router = new(Backbone.Router.extend({ 
 

 
    routes: { 
 
    "authentication": "authentication", 
 
    "contacts": "contacts", 
 
    "*notFound": "notFound" 
 
    }, 
 

 
    /** 
 
    * @param {string} routeName 
 
    */ 
 
    disableRoute: function(routeName) { 
 
    var index, handler, handlers = Backbone.history.handlers; 
 
    delete this.routes[routeName]; 
 
    for (var i = 0, len = handlers.length; i < len; i++) { 
 

 
     handler = handlers[i]; 
 
     if (handler.route.toString() === router._routeToRegExp(routeName).toString()) { 
 
     handlers.splice(index, 1); 
 
     break; 
 
     } 
 
    } 
 
    }, 
 

 
    contacts: function() { 
 
    alert('route `contacts`'); 
 
    }, 
 
    authentication: function() { 
 
    alert('route `authentication`'); 
 
    }, 
 
    notFound: function() { 
 
    alert('route `notFound`'); 
 
    router.navigate('404'); 
 
    } 
 
})); 
 

 
Backbone.history.start({ 
 
    silent: true 
 
}); 
 

 
$(function() { 
 
    $('#remove').on('click', function() { 
 
    router.disableRoute('authentication'); 
 

 
    router.navigate('404'); 
 
    }); 
 

 
    $('#goto_auth').on('click', function() { 
 
    router.navigate('authentication', { 
 
     trigger: true 
 
    }); 
 
    }); 
 

 
    $('#goto_contacts').on('click', function() { 
 
    router.navigate('contacts', { 
 
     trigger: true 
 
    }); 
 
    }); 
 
});
button { 
 
    display: block; 
 
    margin: 10px; 
 
}
<html> 
 

 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script> 
 

 
</head> 
 

 
<body> 
 
    <button id="goto_auth">goto authentication route</button> 
 
    <button id="goto_contacts">goto contacts route</button> 
 
    <hr> 
 
    <button id="remove">remove authentication route</button> 
 
</body> 
 

 
</html>

İlgili konular