2013-11-27 38 views
0

我在我的代碼中多次使用$ http服務。我不喜歡它,因爲我正在考慮將來切換到websockets,所以我需要一些通用接口來進行通信。角js中的某種通信門面是否有任何模式?哪裏可以輕鬆順利地在一個地方切換通信方式?

回答

1

最簡單的方法是包裹通信創建service

mainModule.factory("serverCommunication", function($q){ 

    return function(message) { 

     // here goes the logic of communication 

     // switch by message.type to handle the communication properly 

     // should return a promise (exactly as '$http' does) 
    } 

}) 

其中message可能包含的所有信息,例如:

message = { 
    url : 'url', 
    data : {...}, 
    type : 'type', 
    parameters : {...} 
    ... 
} 
+0

我喜歡它,我給+,我已經一直在想這樣的事情。我一直在琢磨是否有任何常用的溶劑已經存在。感謝您輸入artur – speedingdeer