angular-pusher
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -23,61 +23,61 @@ /** | ||
.provider('PusherService', function () { | ||
var scriptUrl = '//js.pusher.com/2.1/pusher.min.js'; | ||
var scriptId = 'pusher-sdk'; | ||
var apiKey = ''; | ||
var initOptions = {}; | ||
var scriptUrl = '//js.pusher.com/2.1/pusher.min.js'; | ||
var scriptId = 'pusher-sdk'; | ||
var apiKey = ''; | ||
var initOptions = {}; | ||
this.setPusherUrl = function (url) { | ||
scriptUrl = url || scriptUrl; | ||
return this; | ||
}; | ||
this.setPusherUrl = function (url) { | ||
scriptUrl = url || scriptUrl; | ||
return this; | ||
}; | ||
this.setOptions = function (options) { | ||
initOptions = options || initOptions; | ||
return this; | ||
}; | ||
this.setOptions = function (options) { | ||
initOptions = options || initOptions; | ||
return this; | ||
}; | ||
this.setToken = function (token) { | ||
apiKey = token || apiKey; | ||
return this; | ||
}; | ||
this.setToken = function (token) { | ||
apiKey = token || apiKey; | ||
return this; | ||
}; | ||
// load the pusher api script async | ||
function createScript ($document, callback, success) { | ||
var tag = $document.createElement('script'); | ||
tag.type = 'text/javascript'; | ||
tag.async = true; | ||
tag.id = scriptId; | ||
tag.src = scriptUrl; | ||
// load the pusher api script async | ||
function createScript ($document, callback, success) { | ||
var tag = $document.createElement('script'); | ||
tag.type = 'text/javascript'; | ||
tag.async = true; | ||
tag.id = scriptId; | ||
tag.src = scriptUrl; | ||
tag.onreadystatechange = tag.onload = function () { | ||
var state = tag.readState; | ||
if (!callback.done && (!state || /loaded|complete/.test(state))) { | ||
callback.done = true; | ||
callback(); | ||
} | ||
}; | ||
tag.onreadystatechange = tag.onload = function () { | ||
var state = tag.readState; | ||
if (!callback.done && (!state || /loaded|complete/.test(state))) { | ||
callback.done = true; | ||
callback(); | ||
} | ||
}; | ||
$document.getElementsByTagName('head')[0].appendChild(tag); | ||
} | ||
$document.getElementsByTagName('head')[0].appendChild(tag); | ||
} | ||
this.$get = ['$document', '$timeout', '$q', '$rootScope', '$window', | ||
function ($document, $timeout, $q, $rootScope, $window) { | ||
var deferred = $q.defer(); | ||
var socket; | ||
var pusher; | ||
this.$get = ['$document', '$timeout', '$q', '$rootScope', '$window', | ||
function ($document, $timeout, $q, $rootScope, $window) { | ||
var deferred = $q.defer(); | ||
var socket; | ||
var pusher; | ||
function onSuccess () { | ||
pusher = new $window.Pusher(apiKey, initOptions); | ||
} | ||
function onSuccess () { | ||
pusher = new $window.Pusher(apiKey, initOptions); | ||
} | ||
var onScriptLoad = function (callback) { | ||
onSuccess(); | ||
$timeout(function () { | ||
deferred.resolve(pusher); | ||
}); | ||
}; | ||
var onScriptLoad = function (callback) { | ||
onSuccess(); | ||
$timeout(function () { | ||
deferred.resolve(pusher); | ||
}); | ||
}; | ||
createScript($document[0], onScriptLoad); | ||
return deferred.promise; | ||
}]; | ||
createScript($document[0], onScriptLoad); | ||
return deferred.promise; | ||
}]; | ||
@@ -87,16 +87,23 @@ }) | ||
.factory('Pusher', ['$rootScope', 'PusherService', | ||
function ($rootScope, PusherService) { | ||
return { | ||
subscribe: function (channelName, eventName, callback) { | ||
PusherService.then(function (pusher) { | ||
var channel = pusher.channel(channelName) || pusher.subscribe(channelName); | ||
channel.bind(eventName, function (data) { | ||
if (callback) callback(data); | ||
$rootScope.$broadcast(channelName + ':' + eventName, data); | ||
$rootScope.$digest(); | ||
}); | ||
}); | ||
} | ||
}; | ||
} | ||
function ($rootScope, PusherService) { | ||
return { | ||
subscribe: function (channelName, eventName, callback) { | ||
PusherService.then(function (pusher) { | ||
var channel = pusher.channel(channelName) || pusher.subscribe(channelName); | ||
channel.bind(eventName, function (data) { | ||
if (callback) callback(data); | ||
$rootScope.$broadcast(channelName + ':' + eventName, data); | ||
$rootScope.$digest(); | ||
}); | ||
}); | ||
}, | ||
unsubscribe: function (channelName) { | ||
PusherService.then(function (pusher) { | ||
pusher.unsubscribe(channelName); | ||
}); | ||
} | ||
}; | ||
} | ||
]); |
@@ -1,1 +0,1 @@ | ||
"use strict";angular.module("doowb.angular-pusher",[]).provider("PusherService",function(){function createScript($document,callback){var tag=$document.createElement("script");tag.type="text/javascript",tag.async=!0,tag.id=scriptId,tag.src=scriptUrl,tag.onreadystatechange=tag.onload=function(){var state=tag.readState;callback.done||state&&!/loaded|complete/.test(state)||(callback.done=!0,callback())},$document.getElementsByTagName("head")[0].appendChild(tag)}var scriptUrl="//js.pusher.com/2.1/pusher.min.js",scriptId="pusher-sdk",apiKey="",initOptions={};this.setPusherUrl=function(url){return scriptUrl=url||scriptUrl,this},this.setOptions=function(options){return initOptions=options||initOptions,this},this.setToken=function(token){return apiKey=token||apiKey,this},this.$get=["$document","$timeout","$q","$rootScope","$window",function($document,$timeout,$q,$rootScope,$window){function onSuccess(){pusher=new $window.Pusher(apiKey,initOptions)}var pusher,deferred=$q.defer(),onScriptLoad=function(){onSuccess(),$timeout(function(){deferred.resolve(pusher)})};return createScript($document[0],onScriptLoad),deferred.promise}]}).factory("Pusher",["$rootScope","PusherService",function($rootScope,PusherService){return{subscribe:function(channelName,eventName,callback){PusherService.then(function(pusher){var channel=pusher.channel(channelName)||pusher.subscribe(channelName);channel.bind(eventName,function(data){callback&&callback(data),$rootScope.$broadcast(channelName+":"+eventName,data),$rootScope.$digest()})})}}}]); | ||
"use strict";angular.module("doowb.angular-pusher",[]).provider("PusherService",function(){function createScript($document,callback){var tag=$document.createElement("script");tag.type="text/javascript",tag.async=!0,tag.id=scriptId,tag.src=scriptUrl,tag.onreadystatechange=tag.onload=function(){var state=tag.readState;callback.done||state&&!/loaded|complete/.test(state)||(callback.done=!0,callback())},$document.getElementsByTagName("head")[0].appendChild(tag)}var scriptUrl="//js.pusher.com/2.1/pusher.min.js",scriptId="pusher-sdk",apiKey="",initOptions={};this.setPusherUrl=function(url){return scriptUrl=url||scriptUrl,this},this.setOptions=function(options){return initOptions=options||initOptions,this},this.setToken=function(token){return apiKey=token||apiKey,this},this.$get=["$document","$timeout","$q","$rootScope","$window",function($document,$timeout,$q,$rootScope,$window){function onSuccess(){pusher=new $window.Pusher(apiKey,initOptions)}var pusher,deferred=$q.defer(),onScriptLoad=function(){onSuccess(),$timeout(function(){deferred.resolve(pusher)})};return createScript($document[0],onScriptLoad),deferred.promise}]}).factory("Pusher",["$rootScope","PusherService",function($rootScope,PusherService){return{subscribe:function(channelName,eventName,callback){PusherService.then(function(pusher){var channel=pusher.channel(channelName)||pusher.subscribe(channelName);channel.bind(eventName,function(data){callback&&callback(data),$rootScope.$broadcast(channelName+":"+eventName,data),$rootScope.$digest()})})},unsubscribe:function(channelName){PusherService.then(function(pusher){pusher.unsubscribe(channelName)})}}}]); |
{ | ||
"name": "angular-pusher", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "brian.woodward@gmail.com" |
@@ -10,3 +10,3 @@ { | ||
"name": "angular-pusher", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "", | ||
@@ -13,0 +13,0 @@ "homepage": "", |
7153
118