Last active
August 31, 2017 00:22
-
-
Save dhwang/58748c407fdcba1fa8a46428f3ccd6f4 to your computer and use it in GitHub Desktop.
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
app.service('appPubSub', function($window) { | |
this.subscribe = function(subject, cb) { | |
$window.addEventListener('message', function(event) { | |
if (typeof(event.data.message) === 'string') { | |
event.data.message = JSON.parse(event.data.message.replace(/\&dquot/g, '"').replace(/\&squot/g, "'")); | |
} | |
return event.data.subject === subject && cb(event.data.message); | |
}); | |
}; | |
this.publish = function(subject, msg) { | |
var msgStr = JSON.stringify(msg).replace(/[']/g, '&squot').replace(/["]/g, '&dquot'); | |
$window.postMessage({ subject: subject, message: msgStr }); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment