在Backbone.js中有条件地执行路由
Conditionally Execute Routes in Backbone.js
为了在backbone.js中实现ACL,我正在寻找一种根据某些函数的结果有条件地触发路由的方法。我应该使用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
}
});
使用router.navigate()
方法可以使用不同的路由。您需要将{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
}
});
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Emberjs应用程序加载在除Index之外的所有路由上
- JavaScript执行暂时挂起页面
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 当js函数's已执行
- 哪个先执行
- 如何在路由更改后停止 setInterval 函数在角度指令上执行
- 如何让 angularJS 路由提供程序在路由更改之前执行操作
- 在带有视图(路由)的 AngularJS 中,为什么控制器只向前执行
- 余烬 了解路由/控制器之间的执行流
- 有没有一种方法可以在express.js中的url路由中执行动态键/值参数
- 使用新查询参数执行完整路由转换时Ember TransitionAborted
- 如何在第一个匹配的路由发送响应后结束node.js的执行
- Rails路由执行两次
- 如何依次为父路由和子路由执行resolve
- 在Meteor / Flow Router中的每条路由都重新执行Javascript
- 我该如何在angular应用的路由生效之前执行AJAX请求呢?
- ExpressJs在为每个路由发送响应后执行回调
- 使用超测运行Mocha测试时,Express路由永远不会执行
- 在Backbone.js中有条件地执行路由