From 52bad4f9b59667b67a4f91a0fbb9865fb74004ca Mon Sep 17 00:00:00 2001 From: Jan Pantel Date: Thu, 7 Nov 2013 09:07:30 +0100 Subject: [PATCH] Refactored to Angular service --- angular-sails.js | 120 +++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 62 deletions(-) diff --git a/angular-sails.js b/angular-sails.js index 84cb34f..8eec168 100644 --- a/angular-sails.js +++ b/angular-sails.js @@ -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, @@ -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)); \ No newline at end of file