Browse files

refactoring, bugfix on socket service

  • Loading branch information...
1 parent 9fdb581 commit 465e4242c4494e2f4dc357d33085f263f66b5225 @btford committed Jul 18, 2012
Showing with 22 additions and 5 deletions.
  1. +1 −1 public/js/app.js
  2. +0 −2 public/js/controllers.js
  3. +21 −2 public/js/services.js
View
2 public/js/app.js
@@ -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});
View
2 public/js/controllers.js
@@ -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'];
View
23 public/js/services.js
@@ -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.