Skip to content

Commit

Permalink
Refactored to Angular service
Browse files Browse the repository at this point in the history
  • Loading branch information
janpantel committed Nov 7, 2013
1 parent eb51e3d commit 52bad4f
Showing 1 changed file with 58 additions and 62 deletions.
120 changes: 58 additions & 62 deletions angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'use strict';
var ngSailsModule = angular.module('ngSails', []);

function $SailsProvider() {
ngSailsModule.service('$sails', ['$rootScope', function ($rootScope) {

var socket = io.connect(),
connected = false,
Expand All @@ -30,74 +30,70 @@
connected = false;
});

this.$get = function ($rootScope) {
return {
reconnect: function (url, options) {
var exec = function () {
socket.disconnect();
if (socket.socket !== undefined) {
socket.socket.connect(url, options);
}
};
//If we are connected, we can execute the reconnect right away.
//If we are not, we queue it up, to execute it during to connect event occured.
if (connected === true) {
exec();
} else {
reconnectAttempt = exec;
return {
reconnect: function (url, options) {
var exec = function () {
socket.disconnect();
if (socket.socket !== undefined) {
socket.socket.connect(url, options);
}
};
//If we are connected, we can execute the reconnect right away.
//If we are not, we queue it up, to execute it during to connect event occured.
if (connected === true) {
exec();
} else {
reconnectAttempt = exec;
}

},
disconnect: function () {
socket.disconnect();
},
emit: function (event, data) {
socket.emit(event, data);
},
on: function (event, cb) {
socket.on(event, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
},
disconnect: function () {
socket.disconnect();
},
emit: function (event, data) {
socket.emit(event, data);
},
on: function (event, cb) {
socket.on(event, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
},
get: function (url, cb) {
socket.get(url, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
});
},
get: function (url, cb) {
socket.get(url, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
},
post: function (url, data, cb) {
socket.post(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
});
},
post: function (url, data, cb) {
socket.post(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
},
put: function (url, data, cb) {
socket.put(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
});
},
put: function (url, data, cb) {
socket.put(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
},
delete: function (url, data, cb) {
socket.delete(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
});
},
delete: function (url, data, cb) {
socket.delete(url, data, function () {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
}
};
});
}
};
}

ngSailsModule.provider('$sails', $SailsProvider);
}]);

}(angular, io));

0 comments on commit 52bad4f

Please sign in to comment.