Last active
August 29, 2015 14:09
-
-
Save monkeymonk/a9026450f91c2e0ca6f8 to your computer and use it in GitHub Desktop.
AngularJS simple PubSub service
This file contains hidden or 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
'use strict'; | |
(function() { | |
var mod = angular.module("App.services", []); | |
//register other services here... | |
/* pubsub - based on https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js*/ | |
mod.factory('pubsub', function() { | |
var cache = {}; | |
return { | |
publish: function(topic, args) { | |
cache[topic] && $.each(cache[topic], function() { | |
this.apply(null, args || []); | |
}); | |
}, | |
subscribe: function(topic, callback) { | |
if(!cache[topic]) { | |
cache[topic] = []; | |
} | |
cache[topic].push(callback); | |
return [topic, callback]; | |
}, | |
unsubscribe: function(handle) { | |
var t = handle[0]; | |
cache[t] && d.each(cache[t], function(idx){ | |
if(this == handle[1]){ | |
cache[t].splice(idx, 1); | |
} | |
}); | |
} | |
} | |
}); | |
return mod; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment