未捕获的TypeError:无法读取属性' host'显示/关闭模态对话框时Durandal中未定义的

时间:2014-05-25 17:31:23

标签: durandal

使用Durandal 2.0.1(由于项目开发的限制,无法更新到2.1.0),我对此问题标题中显示的错误有间歇性问题。

我正在做的就是定义一个自定义对话框然后显示它:

    var pleaseWaitModal = new modalBusy();
    dialog.show(pleaseWaitModal);

当我的ajax电话结束时,我做了:

    dialog.close(pleaseWaitModal);

...然后显示另一个带有我的ajax调用结果的模态。

如果ajax调用需要半秒到一秒,这一切都能正常工作。如果这是一个更快的调用,那么我在控制台窗口中得到Uncaught TypeError: Cannot read property 'host' of undefined。盒子仍然关闭,只是我得到一个恐慌的项目经理询问红色错误是什么......

这纯粹是因为我试图在“dialog.show()”在某些情况下正确完成之前运行“dialog.close()”吗?

事件的顺序基本上是:

*user instigates action requiring a detailed modal dialog to appear with data in it
*as it takes several seconds to populate on some occasions, an interim modal dialog is shown with "please wait" in it
*once the ajax request is complete, the "pleasewait" modal is closed and the "detail" modal is shown

*so a bit like:
    var pleaseWaitModal = new modalBusy();
    dialog.show(pleaseWaitModal);

    //set up deferred calls for ajax data and call ...
    var deferredAjax = callDataFunction(myparams...);
    return deferredAjax.then(function(result) {
         dialog.close(pleaseWaitModal);
         var detailModal = new detailModal();
         detailModal.show(result);
    });

所以我认为我不能使用dialog.show(pleaseWaitModal)电话上的承诺来实现这一点,是吗?

1 个答案:

答案 0 :(得分:0)

你是否正在使用从dialog.close函数返回的promise来打开你的新模态?你可以试试这个:

从最初的对话框开始:

dialog.show(new modal()).then(function(responseData) { 
    dialog.show(new pleaseWaitModal(responseData)); 
});

我认为您遇到的问题是异步时序相关,这就是为什么使用延期作品的原因。

编辑:与我在下面的评论相关,你可能会看一下只使用一个模态,并在其中添加一个加载指示器,如下所示:

view.html

<div data-bind="visible: isLoading">
    <h1>Please wait...</h1>
    <i class="icon-spin icon-spinner icon-4x"></i>
</div>

modalViewModel.js

var vm = {
    isLoading: ko.observable(true)
};

vm.activate = function() {
    makeAjaxCall().then(function(data) {
        vm.isLoading(false);
        **Do whatever you need for your ajax return**
        return true;
    });
});

我认为这应该可以满足您的需求。

相关问题