简体   繁体   English

在Backbone.js中有条件地执行路由

[英]Conditionally Execute Routes in Backbone.js

Working towards implementing ACL in backbone.js, I was looking for a method to conditionally trigger routes based on the outcome of some function. 我致力于在ebrian.js中实现ACL,我正在寻找一种根据某些功能的结果有条件地触发路由的方法。 Should I use execute or route ? 我应该使用execute还是route?

 function isRouteAuthorized(route, name) { // returns true or false depending on some conditions } Backbone.Router.extend({ routes: {"": "users", "resources": "resources",}, route: function (route, name, callback) { if (isRouteAuthorized(route, name)) { //follow to route // How to achieve this ?? } else { //go to error route // How to achieve this ?? } }, users: function () { //display users view }, resources: function () { //display resources view }, error: function () { //display error view } }); 

Use the router.navigate() method to use a different route. 使用router.navigate()方法使用其他路由。 You'd need to pass {trigger: true} as an option to it so that it invokes the specified router method as well. 您需要将{trigger: true}作为选项传递给它,以便它也调用指定的路由器方法。

 Backbone.Router.extend({ routes: {"": "users", "resources": "resources",}, execute: function (callback, name, args) { if (condition) { //follow to route callback.apply(this, args); } else { //go to error route this.navigate('error', {trigger: true}); } return false; }, users: function () { //display users view }, resources: function () { //display resources view }, error: function () { //display error view } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM