سؤال

So I have Form*Handler*.js which has function:

FormHelper.prototype.foldUnfoldForm = function($container, state) {
    main.addressDAO.get(debtorCasePath, viewEntityId).done(function(address) {
        return brite.display('case/' + state + 'AddressView', $container, address).done(function() {
            $container.children().unwrap();
        })
    });
};

brite.display does return a deferred object. If I console.log this whole code, it I can see it is a deferred object with all the methods (pipe, done, faile, etc).

In Form*Manager*.js, I just call the function in Form*Helper*:

FormManager.prototype.foldUnfoldForm = function($container, state) {
    return this.FormHelper.foldUnfoldForm($container, state);
};

My problem is that FormManager.prototype.foldUnfoldForm returns undefined. If I debug or console log to this point, the returned value is undefined. How can it be undefined if what I return in FormHelper.prototype.foldUnfoldForm is not undefined?

EDIT: The deferred object is actually returned before it has resolved, which makes it undefined in the functions that calls it. How do I avoid that and return the deferred object only when it is resolved? I am trying to do .done to the returned value, but it says Uncaught TypeError: Cannot call method 'done' of undefined, because it is undefined...

هل كانت مفيدة؟

المحلول

You can't return anything from the done callback of a promise object. Instead, you should use .then and return the original deferred from the function so that you will have a deferred object.

FormHelper.prototype.foldUnfoldForm = function($container, state) {
    return main.addressDAO.get(debtorCasePath, viewEntityId).then(function(address) {
        return brite.display('case/' + state + 'AddressView', $container, address).done(function() {
            $container.children().unwrap();
        })
    });
};

This assumes main.addressDAO.get() is returning a jquery promise object. If it isn't then we'll need to know what kind of promise/deferred object it is returning.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top