Durandal基于userRole构建路由

时间:2014-02-27 19:44:28

标签: javascript durandal

我在这里做错了什么想要建立一个基于userRole的导航 更新:结果是对象 console.log(结果); enter image description here console.log(JSON.stringify(result)); 给我回复

function activate() {
    console.log(appsecurity.userInfo());

    if (typeof (appsecurity.userInfo()) === 'Administrator'){
        return privateAccess();

    } else {
        return publicAccess();
    }

    router.on('router:route:not-found', function (fragment) {
        logError('No Route Found', fragment, true); 
    });
}

var courses = [
    { route: '', title:'Welcome', moduleId: 'viewmodels/welcome', nav: true, type: 'guest' , authorize: ["User", "Administrator"]},
    { route: 'flickr', moduleId: 'viewmodels/flickr', nav: true, authorize: ["User"] },
    { route: 'youtube', moduleId: 'viewmodels/youtube', nav: true, type: 'member' },
    { route: 'facebook', moduleId: 'viewmodels/facebook', nav: true, authorize: ["Administrator"] },
    { route: 'skype', moduleId: 'viewmodels/skype', nav: true, authorize: ["User", "Administrator"] }
] ;  

var acceptedTypes = ["Administrator", "Visitor"];

var result = (function(authTypes) {
    var results = [], i, j, k, works;

    for(i=0; i<courses.length; i++) {
        if(courses[i]&&courses[i].authorize&&courses[i].authorize.length) {
            works = false;
            for(j=0; j<courses[i].authorize.length; j++) {
                for(k=0; k<authTypes.length; k++) {
                    works = works || courses[i].authorize[j] === authTypes[k];
                }
            }

            if(works)
                results.push(courses[i]);
        }
    }

    return results;
}) (acceptedTypes);

console.log(JSON.stringify(result)); 

function privateAccess() {
    return router.map(result)            // Map the routes
        .buildNavigationModel() // Finds all nav routes and readies them
        //TODO!?: .mapUnknownRoutes("notfound","notfound")
        .mapUnknownRoutes('account/login', 'not-found')
        .activate();            // Activate the router
        //.activate({ pushState: true });
}

然后这将无效 data-bind =&#34; foreach:router.navigationModel&#34; EMPTY ,我看不到任何错误控制台

var routesArray = JSON.stringify(result);
router.makeRelative({ moduleId: 'viewmodels' }); // router will look here for viewmodels by convention
router.map(routesArray).buildNavigationModel();

return router.makeRelative({ moduleId: 'viewmodels' }).map(routesArray).mapUnknownRoutes('account/login', 'not-found').buildNavigationModel();

但这将有效

router.makeRelative({ moduleId: 'viewmodels' }); // router will look here for viewmodels by convention

router.map([
    { route: '', title:'Welcome', moduleId: 'viewmodels/welcome', nav: true, type: 'guest' , authorize: ["User", "Administrator"]},
    { route: 'flickr', moduleId: 'viewmodels/flickr', nav: true, authorize: ["User"] },
    { route: 'youtube', moduleId: 'viewmodels/youtube', nav: true, type: 'member' },
    { route: 'facebook', moduleId: 'viewmodels/facebook', nav: true, authorize: ["Administrator"] }
    { route: 'skype', moduleId: 'viewmodels/skype', nav: true, authorize: ["User", "Administrator"] }
]).buildNavigationModel();

return router;

2 个答案:

答案 0 :(得分:1)

来自@ mike-samuel的评论是对的。 JSON.stringify会返回一个字符串。将字符串传递给路由器时,它假定该字符串表示路由。这是router.map的代码;在您的情况下,您使用'route'作为字符串,并且您没有传入'config'对象,因此路由器将为您创建一个配置并将您的“route”(字符串化数组)设置为路由参数:

router.map = function(route, config) {
    if (system.isArray(route)) {
        for (var i = 0; i < route.length; i++) {
            router.map(route[i]);
        }

        return router;
    }

    if (system.isString(route) || system.isRegExp(route)) {
        if (!config) {
            config = {};
        } else if (system.isString(config)) {
            config = { moduleId: config };
        }

        config.route = route;
    } else {
        config = route;
    }

    return mapRoute(config);
};

路由器稍后会继续尝试从你的数组中创建一个正则表达式,我只能想象从这里开始变得混乱。

您使用JSON.stringify将任何对象转换为“数组”(实际上将其转换为字符串)似乎很可疑。事先是什么对象?如果它已经是一个数组,那么你应该能够将它直接传递给路由器:

// Map the result directly - do not stringify!
router.map(result).buildNavigationModel();

或者,如果结果是字符串,那么你可能想要做一个JSON.parse:

// PARSE the result string as an array
var routesArray = JSON.parse(result);
router.makeRelative({ moduleId: 'viewmodels' });
router.map(routesArray).buildNavigationModel();

顺便说一下,我看不到你的router被宣布的位置,但是我认为你正在某个地方把它作为子路由器,而不是试图使用根路由器?

router = rootRouter.createChildRouter()

修改

根据您更新的代码,我将其复制到我的应用程序中,如下所示:

var courses = [
    { route: '', title: 'Welcome', moduleId: 'viewmodels/welcome', nav: true, type: 'guest', authorize: ["User", "Administrator"] },
    { route: 'flickr', moduleId: 'viewmodels/flickr', nav: true, authorize: ["User"] },
    { route: 'youtube', moduleId: 'viewmodels/youtube', nav: true, type: 'member' },
    { route: 'facebook', moduleId: 'viewmodels/facebook', nav: true, authorize: ["Administrator"] },
    { route: 'skype', moduleId: 'viewmodels/skype', nav: true, authorize: ["User", "Administrator"] }
];

var acceptedTypes = ["Administrator", "Visitor"];

var result = (function (authTypes) {
    var results = [], i, j, k, works;

    for (i = 0; i < courses.length; i++) {
        if (courses[i] && courses[i].authorize && courses[i].authorize.length) {
            works = false;
            for (j = 0; j < courses[i].authorize.length; j++) {
                for (k = 0; k < authTypes.length; k++) {
                    works = works || courses[i].authorize[j] === authTypes[k];
                }
            }

            if (works)
                results.push(courses[i]);
        }
    }

    return results;
})(acceptedTypes);

console.log(JSON.stringify(result));

var child = rootRouter.createChildRouter();
child.map(result).buildNavigationModel();

var nav = child.navigationModel;

debugger;

在调试器中,当我评估nav()时,其中有三个链接,我能够将它们绑定到我的视图中。您的原始源代码中有一些不匹配的引号 - 也许这就是扔你的东西?

答案 1 :(得分:0)

您记录console.log(JSON.stringify(result));,但请使用JSON.stringify(resultSetB)

resultSetB 而不是结果