jQuery Deferred,在链中获取值时遇到麻烦

时间:2012-05-17 14:35:18

标签: javascript jquery jquery-deferred

buildMyUrl的结果总是在调用函数中未定义。我不确定我在做什么错误链接这些功能:

var buildMyUrl = function (callback)
{   
    var param;

    var addParams = function (domainUrl)
    {
        domainUrl += "page.aspx?";
        domainUrl += "Param1=";
    };

    $.when(self.buildDomainUrl()).then(addParams);
};

buildDomainUrl返回JQuery承诺,添加参数工作正常。 现在我有一个想要使用buildMyUrl的结果的函数:

    var buildAPageUrl  = function ()
    {
        var dfd = $.Deferred();

        var addCalendarPage = function (domainUrl)
        {
             dfd.resolve(domainUrl += "/Calendar.aspx?");
        };

   // Here lies the problem (buildMyUrl is undefined)
        $.when(buildMyUrl()).then(addCalendarPage);

        return dfd.promise();
    }

1 个答案:

答案 0 :(得分:0)

以下作品基于Kevin B的评论:

var buildMyUrl = function (callback)
{   
    var dfd = $.Deferred();
    var param;

    var addParams = function (domainUrl)
    {
        domainUrl += "page.aspx?";
        domainUrl += "Param1=";
        dfd.resolve();
    };

    $.when(self.buildDomainUrl()).then(addParams);
    return dfd.promise();
};