Created
October 5, 2013 16:29
-
-
Save mjc-gh/6843004 to your computer and use it in GitHub Desktop.
Simple EventSource factory object for angular.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(factories){ | |
factories.factory('$eventSource', ['$window', function($window){ | |
var EventSource = $window.EventSource; | |
function parse(obj){ | |
try { var json = JSON.parse(obj); } | |
catch(e) { json = {}; } | |
finally { return json; } | |
} | |
return function(url){ | |
var esrc = new EventSource(url); | |
return { | |
on:function(type, callback){ | |
esrc.addEventListener(type, function(ev){ | |
callback(parse(ev.data)); | |
}); | |
} | |
}; | |
} | |
}]); | |
}(angular.module('gp.Factories')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment