Durandal Modal Won't Close

2019-07-19 10:43发布

问题:

Modals are turning out to be more difficult than I thought :/

Got the modal loading up a view / view modal properly, clicking the save button saves the information (I do get a 'Should be Empty : []' from Q.js but apparently this isn't a problem?) the problem I am having is probably related to promises but if it is I can't find it.

Parent's view model -

var createNew = function () {
    app.showModal(tfcreate).then(function (modalResult) {
        if (!modalResult) { return false; }

        var templateId = modalResult;
        router.replaceLocation('#/templateformedit/' + templateId);
    });
};

Modal's view model -

var cancel = function () {
    this.modal.close(false);
};

var save = function () {
    isSaving(true);
    setRevisionInfo();

    datacontext.saveChanges()
        .then(alertMe)
        .fail(initFailed)
        .fin(complete);

    function setRevisionInfo() {
        templateForm().revisionLevel(1);
        templateForm().createdById(shell.currentUser().id());
        templateForm().lastRevisedId(shell.currentUser().id());
        var nowDT = moment().format('LL');
        templateForm().lastRevisedDT(nowDT);
        templateForm().createdDT(nowDT);            
    }

    function alertMe() {
        return console.log('done');  // <<< This is firing ok
    }

    function complete() {
        isSaving(false);
        this.modal.close(templateForm().id());    // <<< Breakpoint reaches here just fine 
    }
};

If I press the cancel button which is bound back to cancel() it closes just fine, if I click the save button it hits save(), saves the object properly, and reaches all breakpoints but never closes. If after I save I press cancel it closes just fine again. I have tried calling cancel() during the complete() function and it reaches the statement, but again does not close. Any ideas???

Note : I can call router.replaceLocation from the modal and it will change the view just fine but the modal persists to the next view.

Edit : I added another button 'close' that is disabled until isSaving is finished and hasChanges is false and that lets me close it and everything just fine, but that shouldn't be necessary, right?

回答1:

Per request:

Are you sure that this in complete() is still your vm context? Try var self=this; at the top of save() and in complete() self.modal.close(...)