十字路口和要求

时间:2012-12-27 22:04:48

标签: requirejs

我在使用needjs的十字路口获得匹配路线时遇到了麻烦。好吧,它可能与requirejs无关,只是我想提到它。

这就是我的代码:

require.config({
    shim: {
        /* use shims to define dependencies for modules. e.g.,
        'jquery.colorize': ['jquery'],
        'jquery.scroll': ['jquery'],
        */
        'crossroads': ['signals', 'can']
    },
    paths: {
        "jquery": "http://code.jquery.com/jquery-1.8.2",
        "can": "/scripts/can/amd/can",
        "can.fixture": "/scripts/can/amd/can/util/fixture",
        "signals": "/scripts/signals/signals",
        "crossroads": "/scripts/crossroads/crossroads"
    }
});

require(['jquery', 'crossroads', 'controllers/project'], function ($, crossroads, projectController) {
    var projectRoute = crossroads.addRoute('projects', function () {
        $(document).ready(function () {
            console.log('projects ready');
            $.when(ProjectModel.findAll()).then(function (projectResponse) {
                var projects = new SortList(projectResponse);
                console.log('doc ready projects=', projects);

                new ProjectsControl('#projects', {
                    projects: projects,
                    defaultSort: 'priority'
                });
            });
        });
    })
    console.log('matched: ', projectRoute.match(window.location.href));
});

它尝试匹配的网址是http://localhost:34382/projects,而console.log的输出是“匹配:false”

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我发现我需要改变我所匹配的东西。我改变了这一行

console.log('matched: ', projectRoute.match(window.location.href));

到这个

console.log('matched: ', projectRoute.match(window.location.pathname + window.location.search));

现在可行。

还意识到你必须在某处显式调用crossroads.parse()才能工作(或者我错过了什么?)

所以在我的require函数结束时,我调用它并找到了我的路径

crossroads.parse(window.location.pathname + window.location.search);

此外,如果有人想知道,我使用CanJS作为客户端MVC框架,使用ASP.NET MVC4作为服务器端。