Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // We start our app
  2. var myApp = angular.module("myApp", []);
  3.  
  4. myApp.provider('connection', function Connection() {
  5. var connection = $.connection,
  6. reconnectDelay = 1500,
  7. maxReconnectDelay = 60000;
  8.  
  9. // allows you to set logging on before the connection runs
  10. this.showLogging = function () {
  11. $.connection.hub.logging = true;
  12. };
  13.  
  14. // used to override the default values
  15. this.setup = function (queryString, delay, maxDelay) {
  16. reconnectDelay = delay || reconnectDelay;
  17. maxReconnectDelay = maxDelay || maxReconnectDelay;
  18.  
  19. $.connection.hub.qs = queryString;
  20. };
  21.  
  22. // Used to get the connection to add client callbacks, if so desired, in the config stage
  23. this.getConnection = function () {
  24. return connection;
  25. };
  26.  
  27. // This is what is returned when we inject 'connection'
  28. this.$get = ['$q', '$timeout',
  29. function connectionService($q, $timeout) {
  30. var self = this,
  31. failedConnectionAttempts = 0,
  32. loadedDefered = $q.defer(),
  33. isLoaded = loadedDefered.promise,
  34. loading = false,
  35. initialized = false;
  36.  
  37. function whenReady(serverCall) {
  38. return isLoaded.then(function () {
  39. return $q.when(serverCall());
  40. });
  41. }
  42.  
  43. function init() {
  44. // if we are currently loading, abort
  45. if (loading) {
  46. return;
  47. }
  48.  
  49. // if we have not yet been initialized (ie: we are reconnecting)
  50. // then we need to setup the disconnection event
  51. if (!initialized) {
  52. initialized = true;
  53.  
  54. connection.hub.disconnected(function () {
  55. loadedDefered = $q.defer();
  56. isLoaded = loadedDefered.promise;
  57.  
  58. loading = false;
  59. var newDelay = reconnectDelay * (++failedConnectionAttempts);
  60. $timeout(init, Math.min(Math.max(reconnectDelay, newDelay), maxReconnectDelay));
  61. });
  62. }
  63.  
  64. // set loading to be true now that we're handling the connection start
  65. loading = true;
  66.  
  67. connection.hub.start().done(function () {
  68. loading = false;
  69. // resolve the loading defered so that
  70. loadedDefered.resolve();
  71. failedConnectionAttempts = 0;
  72. }).fail(function () {
  73. /// <summary>Panics; figure out what to do here later</summary>
  74. loadedDefered = $q.defer();
  75. isLoaded = loadedDefered.promise;
  76. });
  77. }
  78.  
  79. init();
  80.  
  81. // so that we can say `connection.ready().then(function() { return myServerCall(); });`
  82. self.ready = whenReady;
  83. self.hubs = connection;
  84.  
  85. return self;
  86. }];
  87.  
  88. return this;
  89. });
  90.  
  91. // In your main.js file, or wherever you setup the providers
  92. myApp.config(["connectionProvider",
  93. function (connectionProvider) {
  94. if (window.location.hostname === "localhost") connectionProvider.showLogging();
  95. connectionProvider.setup("browser", 1500, 60000, null);
  96. }]);
  97.  
  98. // An example of moving your client-to-server data into a service
  99. myApp.service('myData', ['connection',
  100. function (connection) {
  101. var self = this,
  102. ready = connection.ready,
  103. myHubServer = connection.hubs.myHub.server;
  104.  
  105. this.getMyData = function () {
  106. return ready(function () {
  107. return myHubServer.getMyData();
  108. });
  109. };
  110.  
  111. return this;
  112. }]);
  113.  
  114. // Which we may then use in the controller
  115. myApp.controller('MyController', ['$scope', 'myData',
  116. function ($scope, myData) {
  117. $scope.someDataArray = [];
  118.  
  119. myData.getMyData().then(function (allMyThings) {
  120. $scope.someDataArray = allMyThings;
  121. });
  122. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement