2010-12-07 34 views
0

上午j确认用户确认。jConfirm警报 - jQuery插件

我的第一个jConfirm不会停止用户操作,而只是传递给下一个。

我的代码:

$(function() { 

    $("#UpdateJobHandler").click(function() { 

     var JobHander = getJobHandler(); 
     if (JobHander.MaxInstances == 0) { 
       jConfirm('Continue?', 'Current Maximum Instances', function (ans) { 
        if (!ans) 
         return; 
       }); 
     } 

     var json = $.toJSON(JobHander); 

     $.ajax({ 
      url: '../Metadata/JobHandlerUpdate', 
      type: 'POST', 
      dataType: 'json', 
      data: json, 
      contentType: 'application/json; charset=utf-8', 
      success: function (data) { 

       var message = data.Message; 
       var alertM = data.MessageType; 
       if (alertM == 'Error') { 
        $("#resultMessage").html(message); 

       } 

       if (alertM == 'Success') { 
        $("#resultMessage").empty(); 
        alert(alertM + '-' + message); 
        action = "JobHandler"; 
        controller = "MetaData"; 
        loc = "../" + controller + "/" + action; 
        window.location = loc; 
       } 

       if (alertM == "Instances") { 
        jConfirm(message, 'Instances Confirmation?', function (answer) { 
         if (!answer) 
          return; 
         else { 
          var JobHandlerNew = getJobHandler(); 
          JobHandlerNew.FinalUpdate = "Yes"; 
          var json = $.toJSON(JobHandlerNew); 
          $.ajax({ 

           url: '../Metadata/JobHandlerUpdate', 
           type: 'POST', 
           dataType: 'json', 
           data: json, 
           contentType: 'application/json; charset=utf-8', 
           success: function (data) { 

            var message = data.Message; 
            $("#resultMessage").empty(); 
            alert(alertM + '-' + message); 
            action = "JobHandler"; 
            controller = "MetaData"; 
            loc = "../" + controller + "/" + action; 
            window.location = loc; 
           } 
          }); 
         } 
        }); 
       } 
      } 
     }); 
    }); 
}); 

我在想什么?

回答

2

不知道这是否就是全部,但这部分:

if (JobHander.MaxInstances == 0) { 
      jConfirm('Continue?', 'Current Maximum Instances', function (ans) { 
       if (!ans) 
        return; 
      }); 
    } 

可能不会做你想做的。它正在退出function(ans) { ... }函数,而您可能想要退出整个处理程序,即$("#UpdateJobHandler").click(function() { ... }。如果是这样,你需要做类似于你在下面做的事情 - 也就是说,在返回之后把整个东西放在function(ans) { ... }之内。可能最好分成更小的功能。

编辑:东西沿着这些路线:

function afterContinue() { 
     var json = $.toJSON(JobHander); 

     $.ajax({ 
      // ... all other lines here ... 
     }); 
    } 

    if (JobHander.MaxInstances == 0) { 
      jConfirm('Continue?', 'Current Maximum Instances', function (ans) { 
       if (ans) { 
        afterContinue(); 
       } 
      }); 
    } 

你可以做类似的事情,所有的success功能。

另一个例子,你可以重写Instances检查这样的:

  function afterInstances() { 
         var JobHandlerNew = getJobHandler(); 
         JobHandlerNew.FinalUpdate = "Yes"; 

         // ... and everything under else branch ... 
      } 

      if (alertM == "Instances") { 
       jConfirm(message, 'Instances Confirmation?', function (answer) { 
        if (answer) { 
         afterInstances(); 
        } 
       }); 
      } 

重要 - 重命名方法(afterContinueafterInstances,...)有一些名字,这意味着什么有用的人在阅读本未来。

+0

感谢icyrock,你可以通过将这些细分为更小的函数来显示样本的重新分解。 – Sreedhar 2010-12-07 06:29:09