2013-05-30 66 views
0

所以我有一個函數通過AJAX提交事物,然後顯示一個對話框,如果它成功與否。所有的工作都很好,但是我希望可以選擇傳遞這個對話函數一個額外的函數(可選),它將執行額外的功能。 這是我有:Jquery:將函數傳遞給一個不需要的函數

// the work 
if (data._response.success == 'true') { 
    $("#suppliers_grid").trigger("reloadGrid"); 
    $('#manage-suppliers-form').fullFormReset(); 
    alertDialog('Supplier '+ action +' successfully!','Success',$('#manage-suppliers-form :input:visible:first').focus()); 
} else { 
    alertDialog('Supplier was not '+ action +' successfully!<br />Please try again or report this to the administrator.','Error','ui-state-error'); 
} 

// the alertDialog function 
function alertDialog(message,title,cssClass,closeFunction) { 
    title = typeof title !== 'undefined' ? title : 'Notice'; 
    cssClass = typeof cssClass !== 'undefined' ? cssClass : 'ui-state-highlight'; 
    if (cssClass=='ui-state-error') { 
    icon = 'ui-icon-alert'; 
    } 
    else { 
    icon = 'ui-icon-info'; 
    } 

    var dialog = $('<div><p><span class="ui-icon '+ icon +'"></span>'+ message +'</p></div>'); 
    dialog.dialog({ 
     modal: true, 
     title: title, 
     buttons: { 
     Ok: function() { $(this).dialog("close"); } 
     }, 
     close: closeFunction() 
    }); 
} 

1)如果我通過一個closeFunction

2)甚至還沒有測試它-without-通過它上面的東西不工作,但我敢肯定它也行不通。 close函數應該是可選的

3)我不能簡單地把alertDialog調用後的'焦點'代碼行。即使這有效(在後臺獲得焦點)。只要有人點擊alertDialog上的「ok」,焦點就會丟失 - 所以需要在alertDialog關閉時調用。

回答

1

首先,無論你在你的問題中提到的(傳遞函數的函數)被稱爲在Javascript編程世界「回調」功能。

順便說一下,回答你的問題,我想你需要在你關閉對話框如下綁定被調用close事件的回調函數:

var that = this; 
    dialog.dialog({ 
     close: function(event, ui) { 
      if(closeFunction && typeof closeFunction === 'function') { 
       closeFunction.call(that); 
      } 
    }}); 

的closeFunction內,儘量做到對焦( )爲所需的元素。

Try to read this!可以更好地理解關閉事件回調的用法。

如果您仍然沒有從我的答案中得到正確的解決方案,請張貼小提琴或更好地理解您面臨的問題!

+0

謝謝,沒有工作 - 但我使用了一些你所提到的,並通過與目標「$(‘#管理,供應商的形式:輸入:可見:第一’)」,而不是的函數「$('#manage-suppliers-form-form:input:visible:first')。focus()」,然後在對話框的關閉部分調用焦點。沒有像傳遞一個函數那麼強大 - 但是對於我現在需要的東西 - 它足夠了。 – SupaMonkey

0

嘗試

// the work 
if (data._response.success == 'true') { 
    $("#suppliers_grid").trigger("reloadGrid"); 
    $('#manage-suppliers-form').fullFormReset(); 
    //Pass the function as the fourth parameter and create a annonymous function to set the focus 
    alertDialog('Supplier '+ action +' successfully!', 'Success', '', function(){ 
     $('#manage-suppliers-form :input:visible:first').focus(); 
    }); 
} else { 
    alertDialog('Supplier was not '+ action +' successfully!<br />Please try again or report this to the administrator.','Error','ui-state-error'); 
} 

// the alertDialog function 
function alertDialog(message,title,cssClass,closeFunction) { 
    title = typeof title !== 'undefined' ? title : 'Notice'; 
    cssClass = typeof cssClass !== 'undefined' ? cssClass : 'ui-state-highlight'; 
    if (cssClass=='ui-state-error') { 
     icon = 'ui-icon-alert'; 
    } 
    else { 
     icon = 'ui-icon-info'; 
    } 

    var dialog = $('<div><p><span class="ui-icon '+ icon +'"></span>'+ message +'</p></div>'); 
    dialog.dialog({ 
     modal: true, 
     title: title, 
     buttons: { 
      Ok: function() { $(this).dialog("close"); } 
     }, 
     close: closeFunction //assign the function reference instead of the return value of the function 
    }); 
}