Is there any disadvantage wrapping socket.io in a callback function in AngularJs Service? -


i made service in angular.js use socket.io inside controllers callback function. in service emitter , listener in same function use in controllers follow.

service.js

(function () { /* socket io service */ var service = angular.module('myservice', ['']);   service.factory('sio', ['$http', function($http){      var socket;     socket = io.connect('http://localhost:3000');      return {         /* queries  user */         login: function(authdata, callback){             socket.emit('__app auth login', authdata);             var response = socket.on('__app auth login', function(res){                 callback("", res);             });             return response;         },         finduser: function(conditions, callback){             socket.emit('__app user find', conditions);             var response = socket.on('__app user find', function(res){                 callback("", res);             });             return response;         },     };   }]); })(); 

my controller

(function () { /* controller */ var mycontroller = angular.module('myctrl', ['']);  mycontroller.controller('mycontroller', function($scope, sio){     var conditions = {username:"john123"};      $scope.conditions = conditions;     $scope.message = "hello";      this.search = function(){         sio.finduser(conditions, function(err, response){             $scope.message = response.status;             console.log(response);         });     }     $scope.search = this.search;   });// /--ctrl })(); 

everything seems work, question if there issue or disadvantage of use that? or should wrap socket.io object in service? in advanced.

there no inherent disadvantage other potential violation of expected behavior depending on accepted best practices. question whether or not in line them.

because actual work (connecting , holding open connected socket) being done in singleton service, don't have worry connections dying.

that said, best practices dictate service-related work should done in service itself. extend callbacks on promises? according john papa angularjs styleguide, promises should returned services can chained in invoking controllers.

as result, based on considered best practice large contingency within angularjs community, implementation not not meet absolute best practices, although reasonable.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -