Advertisement
Guest User

Socket in AngularJS

a guest
Feb 20th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('myApp')
  4.  
  5. .controller('SearchCtrl', function ($scope, socket) {
  6. $scope.results = [];
  7.  
  8. $scope.doSearch = function() {
  9. socket.on('send', function () {
  10. this.results.push('Socket response!');
  11. });
  12. };
  13. })
  14. ;
  15.  
  16. angular.module('myApp').factory('socket', function ($rootScope, socketFactory) {
  17.  
  18. var socket = socketFactory();
  19.  
  20. return {
  21. on: function (eventName, callback) {
  22. socket.on(eventName, function () {
  23. var args = arguments;
  24. $rootScope.$apply(function () {
  25. callback.apply(socket, args);
  26. });
  27. });
  28. },
  29. emit: function (eventName, data, callback) {
  30. socket.emit(eventName, data, function () {
  31. var args = arguments;
  32. $rootScope.$apply(function () {
  33. if (callback) {
  34. callback.apply(socket, args);
  35. }
  36. });
  37. });
  38. }
  39. };
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement