Skip to content

Instantly share code, notes, and snippets.

@zhenghao1
Created June 27, 2014 03:21
Show Gist options
  • Save zhenghao1/a4fe69d80f71284eab0a to your computer and use it in GitHub Desktop.
Save zhenghao1/a4fe69d80f71284eab0a to your computer and use it in GitHub Desktop.

Revisions

  1. floatingmonkey revised this gist Aug 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    (function() {
    var mod = angular.module("App.services", []);

    //insert other service registration here...
    //register other services here...

    /* pubsub - based on https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js*/
    mod.factory('pubsub', function() {
  2. floatingmonkey created this gist Aug 18, 2012.
    39 changes: 39 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    'use strict';

    (function() {
    var mod = angular.module("App.services", []);

    //insert other service registration 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;
    })();