Skip to content

Commit

Permalink
refactoring, bugfix on socket service
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Jul 18, 2012
1 parent 9fdb581 commit 465e424
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/js/app.js
Expand Up @@ -2,7 +2,7 @@


// Declare app level module which depends on filters, and services
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
var app = angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1', controller: MyCtrl1});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2', controller: MyCtrl2});
Expand Down
2 changes: 0 additions & 2 deletions public/js/controllers.js
Expand Up @@ -5,14 +5,12 @@
function AppCtrl($scope, socket) {
socket.on('send:name', function (data) {
$scope.name = data.name;
$scope.$apply();
});
}

function MyCtrl1($scope, socket) {
socket.on('send:time', function (data) {
$scope.time = data.time;
$scope.$apply();
});
}
MyCtrl1.$inject = ['$scope', 'socket'];
Expand Down
23 changes: 21 additions & 2 deletions public/js/services.js
Expand Up @@ -7,7 +7,26 @@
// In this case it is a simple value service.
angular.module('myApp.services', []).
value('version', '0.1').
factory('socket', function () {
factory('socket', function ($rootScope) {
var socket = io.connect();
return socket;
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
});

0 comments on commit 465e424

Please sign in to comment.