无法关闭Durandal模态

时间:2014-04-10 16:02:07

标签: durandal

我几乎已经阅读了关于此问题的每个stackoverflow问题,但仍无法使其正常运行......

我有一个模态我用作"请等待"而异步功能正在发生。

Viewmodel是:

define(['plugins/dialog', 'knockout'], function (dialog, ko) {

    var CustomModal = function () {
        var self = this;
        this.msg = ko.observable(" Please Wait...");
    };

    CustomModal.show = function () {
        return dialog.show(new CustomModal());
    };

    CustomModal.close = function () {
        var self = this;
        dialog.close(self);
    };

    return CustomModal;
});

我还有其他的模式有" ok"按钮,它们关闭得很好,但是这个按钮在我的主要逻辑流程中打开了(我希望,关闭)。

我"要求"我的主代码中的模态视图模型,这可以很好地显示它:

modalBusy.show();

然而,这并没有关闭对话框,它让我疯了!

modalBusy.close();

调试时没有错误,但是" self"在我的close方法中不是一个实际的viewmodel对象,只包含

    var CustomModal = function () {
        var self = this;
        this.msg = ko.observable(" Please Wait...");
    };
再次,显然这是错误的。

1 个答案:

答案 0 :(得分:1)

您忘记使用prototype。你的密切方法应该是:

CustomModal.prototype.close = function() {
    dialog.close(this);
}; 

您编写的方式,close()是一种“静态”方法。然而,对于prototype,它是一种“类”方法。这就是this没有引用你期望的原因。使用原型链,this将引用实例化对象。

更新:以下是如何从主代码中关闭模式的示例,但是,它需要您重构show()方法。

define(['plugins/dialog', 'knockout'], function (dialog, ko) {
  var CustomModal = function () {
    this.msg = ko.observable(" Please Wait...");
  };

  CustomModal.prototype.show = function () {
    return dialog.show(this);
  };

  CustomModal.prototype.close = function () {
    dialog.close(this);
  };

  return CustomModal;
});

// from your main code, you use it like this    
var modal = new modalBusy();
modal.show();

// then, whenever you want to close it
modal.close();
相关问题