Jeremy Noble
Jeremy Noble

Reputation: 183

app.showMessage promise .then fails in HotTowel durandal SPA

Has anyone experienced this, or is able to point me in the right direction:

I'm using code from John Papa's hottowel SPA template. My modal dialog won't close after attempting to cancel changes, using:

  var canDeactivate = function () {
    if (isDeleting()) { return false; }

    if (hasChanges()) {
        var title = 'Do you want to leave "' +
            projectDetails()[0].title() + '" ?';
        var msg = 'Navigate away and cancel your changes?';

        return app.showMessage(title, msg, ['Yes', 'No'])
        .then(confirm);

        function confirm(selectedOption)
        {
            if (selectedOption === 'Yes') {
                cancel();
            }

            return selectedOption;
        }

    }
    return true;
};

I get:

TypeError: activator.deactivateItem(...).then is not a function [Break On This Error]

and a reference to line 72 of Durandal's modalDialog.js:

activator.deactivateItem(instance, true).then(function (closeSuccess) {.....

Upvotes: 1

Views: 1618

Answers (2)

Jeremy Noble
Jeremy Noble

Reputation: 183

It turns out I had some additional code from the template for the HotTowel main.js:

// Q shim
    system.defer = function (action) {
    var deferred = Q.defer();
    action.call(deferred, deferred);
    var promise = deferred.promise;
    deferred.promise = function () {
        return promise;
    };

    return deferred;
    };

This shim was causing the error - don't know why but hope this helps someone...

Upvotes: 1

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

canDeactivate() appears to return either a boolean or a promise.

Typically you would expect such a function always to return a promise or always to return a boolean, otherwise the calling function will have difficulty handling the returned value.

I expect your calling function expects either a boolean or a promise, and throws an error when the other is delivered.

Upvotes: 0

Related Questions