2

I have the jQuery code below, but the when().done() does not work as expected for me. The updateResultFooter() is called bedore the doReverseSearch() method finish her its work. and as a result of that, a button in my view is enabled, and then re-take his default value (desabled) after the replace in the doReverseSearch() method.

$("#idBnSearch").click(function () 
{
    $.when(doReverseSearch(telValue, pageIndex, methodUrl))
    .done(function () 
    {
        updateResultFooter("@ViewBag.CountResult", pageIndex, "@ViewBag.PageCount");
    });

});

function updateResultFooter(resultCount, pageIndex, pageCount) 
{
    if (pageIndex == 0)
        $("#bnPreviousPage").attr('disabled', 'disabled');
    else
        $("#bnPreviousPage").removeAttr('disabled');

    if ((pageIndex + 1) == pageCount)
        $("#bnNextPage").attr('disabled', 'disabled');
    else
        $("#bnNextPage").removeAttr('disabled');
    }

function doReverseSearch(telValue, pageIdx, methodUrl) 
{
    $.ajax(
    {
        url: methodUrl,
        type: 'post',
        data: JSON.stringify({ Telephone: telValue, pageIndex: pageIdx }),
        datatype: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $('#result').replaceWith(data);
        },
        error: function (request, status, err) {
            alert(status);
            alert(err);
        }
    });
}

Thank you in advance

SidAhmed
  • 2,332
  • 2
  • 25
  • 46
  • 4
    `doReverseSearch` doesn't return anything! Try `return $.ajax(...)`. – biziclop Jul 19 '12 at 00:33
  • 1
    `$.when` expects deferred objects as arguments. If you don't pass anything, the callbacks will be invoked immediately. See http://api.jquery.com/jQuery.when/. – Felix Kling Jul 19 '12 at 00:38
  • the return trick solved it. thank you for your help ^^. How can I mark the question as answered ?! – SidAhmed Jul 19 '12 at 00:40

0 Answers0