したがって、ルートが発生しないようにするには、backbone.js
のルータを削除する必要があります。私はmyRouter.off()
とmyRouter.remove()
を試してみました。backbone.jsのルータをアンバインドする
代わりに何をすることができますか?
したがって、ルートが発生しないようにするには、backbone.js
のルータを削除する必要があります。私はmyRouter.off()
とmyRouter.remove()
を試してみました。backbone.jsのルータをアンバインドする
代わりに何をすることができますか?
これは公式にサポートされている方法ではありません。あなたがに任意のルータを無効にしたい場合は、文書化されていないある、Backbone.history.stop();
を使用することができますが、このコメント付きソースコードに現れる:それ以外の場合は
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
// but possibly useful for unit testing Routers.
、あなたはあなたにいくつかのパススルー条件をコーディングする必要があると思いますルーターの状態が "無効"なのか、そうでない場合はルーターのルートハンドラーです。あるいは、文書化されていないBackbone.history.handlers(.routeを含む内部配列 - regexpと.callback)を繰り返して、この特定のルータに関連するルートを削除します。
明らかに、文書化されていないものもありますが、これはBackboneの将来のリリースで変更される可能性があります。
あなたのルーターのインスタンス化を制御することができるならば、あなたは次の操作を行うことができます
var myRouter = new MyRouter({ routes: function(){
return;
}});
あなたがハックベースのソリューションを使用することができます(それは非APIメソッドを使用して、新しいバージョンで作業を停止することがありますBACKBONE.JSの)
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>
おかしい、私はアプリケーションからログアウトした後、それを無効にすると便利です、私を処理するための他の方法があると思うだろうt、でもbackbone.history.stopが本当に好きです – pushplaybang