在Backbone.js

时间:2015-07-02 08:50:34

标签: javascript jquery backbone.js

为了在backbone.js中实现ACL,我一直在寻找一种根据某个函数的结果有条件地触发路由的方法。我应该使用执行还是路由?



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
    }
});




1 个答案:

答案 0 :(得分:3)

使用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
    }
});