2017-03-17 69 views

回答

0

我試圖找出你的意思 - 我覺得基本上你想要某種方式掛接到一個電話,通話結束的開始(加載和卸載微調)?更系統首先用作一次性(第一實例),然後(第二個例子) -

我在兩種不同的方法做到了這一點。希望其中之一將成爲你所需要的。

$.connection.myHub.server.hubMethod().done(function() { 
           //called on success 
          }).fail(function (e) { 
           //called on failure - I don't recommend reading e 
          }).always(function() { 
           //called regardless 
           spinner.close(); 
}); 
spinner.open(); // must be triggerd AFTER call incase exception thrown (due to connection not being up yet) 

如果你不喜歡這樣 - 也許是因爲你打電話樞紐方法在數百碼的不同部分,然後有其他的技巧這是更復雜一點。讓我們看到:

function SetupSpinnerOnCallToSignalrMethod(hubServer, method, spinnerStartCallback, spinnerEndCallback) { 
    var prevFunc = hubServer[method]; 
    hubServer[method] = function() { 
     var ret = prevFunc.apply(this, arguments); 
     spinnerStartCallback(); // must be triggerd AFTER call incase exception thrown (due to connection not being up yet) 
     ret.always(function() { 
      spinnerEndCallback(); 
     }); 
     return ret; 
    }; 
} 

//then call this for each method 
SetupSpinnerOnCallToSignalrMethod($.connection.myHub.server, 
            "hubMethod", 
            function() { spinner.open(); }, 
            function() { spinner.close(); } 
); 

//the server call should then work exactly as before, but the spinner open and close calls are invoked each time. 
相關問題