无法加载路由模块requirejs? durandal bug?

时间:2013-11-27 05:47:35

标签: javascript asp.net-mvc requirejs durandal hottowel

我创建了一个Asp.Net MVC并使用nuget添加HotTowel(2013年9月11日的V2.0.1)。我创建了几个ViewModel,Models。但是,我收到了以下错误。

"无法加载路由模块(viewmodels / myVM)。详细信息:模块加载超时:durandal / plugins / router \ nhttp://requirejs.org/docs/errors.html#timeout"

这是durandal/plugins/router的问题吗?或者它可能是由我添加的一些代码引起的?

错误发生在Scripts / durandal / system.js。

var logError = function(error) {
    if(error instanceof Error){
        throw error;
    }

    throw new Error(error);
};

以下是VM代码。

define(['services/datacontext', 'durandal/plugins/router', 'services/logger'],
// Remove the durandal/plugins/router and the functions will get rid of the error.
function (datacontext, router, logger) {
    var title = 'Event';
    var vm = {
        activate: activate,
        deactivate: deactivate,
        refresh: refresh,
        events: events,
        title: title
    };

    return vm;

    //#region Internal Methods
    var events = ko.observableArray();

    function activate() {
        logger.log(title + ' View Activated', null, title, true);
        return datacontext.getEventPartials(events);
    }

    var deactivate = function () {
        events([]);
    };

    var refresh = function () {
        return datacontext.getEventPartials(events, true);
    };

    //#endregion
});

以下是调用堆栈

logError [system.js] Line 92    Script
Anonymous function [router.js] Line 359 Script
[External Code] 
Anonymous function [system.js] Line 260 Script
[External Code] 
[Async Call]    
    ....

router.js上的代码,

        isProcessing(true);
        router.activeInstruction(instruction);

        if (canReuseCurrentActivation(instruction)) {
            ensureActivation(activator.create(), currentActivation, instruction);
        } else {
            system.acquire(instruction.config.moduleId).then(function(module) {
                var instance = system.resolveObject(module);
                ensureActivation(activeItem, instance, instruction);
            }).fail(function(err){
                    system.error('Failed to load routed module (' + instruction.config.moduleId + '). Details: ' + err.message);
                });
        }
    }

以前的一个在system.js。

    acquire: function() {
        var modules,
            first = arguments[0],
            arrayRequest = false;

        if(system.isArray(first)){
            modules = first;
            arrayRequest = true;
        }else{
            modules = slice.call(arguments, 0);
        }

        return this.defer(function(dfd) {
            require(modules, function() {
                var args = arguments;
                setTimeout(function() {
                    if(args.length > 1 || arrayRequest){
                        dfd.resolve(slice.call(args, 0));
                    }else{
                        dfd.resolve(args[0]);
                    }
                }, 1);
            }, function(err){
                dfd.reject(err);
            });
        }).promise();
    },

1 个答案:

答案 0 :(得分:2)

基于我建议稍微修改vm代码的注释,以便在使用之前定义通过vm返回的所有变量。另外,使用'plugins/router'代替'durandal/plugins/router'

define(['services/datacontext', 'plugins/router', 'services/logger'],
// Remove the durandal/plugins/router and the functions will get rid of the error.
function (datacontext, router, logger) {
    var title = 'Event';
    var events = ko.observableArray();
    var deactivate = function () {
        events([]);
    };
    var refresh = function () {
        return datacontext.getEventPartials(events, true);
    };

    var vm = {
        activate: activate,
        deactivate: deactivate,
        refresh: refresh,
        events: events,
        title: title
    };

    return vm;

    //#region Internal Methods
    function activate() {
        logger.log(title + ' View Activated', null, title, true);
        return datacontext.getEventPartials(events);
    }
    //#endregion
});

BTW名称​​内部方法具有误导性,因为该区域中的所有内容都是通过vm返回的。我更喜欢使用命名函数,如果返回它们,它们会在返回语句之前创建,如果没有返回,则将它们放在内部方法区域中的return语句之下。

define(['services/datacontext', 'plugins/router', 'services/logger'], 
function( datacontext, router, logger ) {
    var title = 'Event';
    var events = ko.observableArray();

    function deactivate () {
        events([]);
    }

    function refresh () {
        return datacontext.getEventPartials(events, true);
    }

    function activate () {
        logger.log(title + ' View Activated', null, title, true);
        return datacontext.getEventPartials(events);
    }

    return {
        activate: activate,
        deactivate: deactivate,
        refresh: refresh,
        events: events,
        title: title
    };

    //#region Internal Methods

    //#endregion
});