2017-03-17 54 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.