簡體   English   中英

在Backbone.js中有條件地執行路由

[英]Conditionally Execute Routes in Backbone.js

我致力於在ebrian.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 } }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM