Skip to content

Instantly share code, notes, and snippets.

@dhwang
Last active August 31, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhwang/58748c407fdcba1fa8a46428f3ccd6f4 to your computer and use it in GitHub Desktop.
Save dhwang/58748c407fdcba1fa8a46428f3ccd6f4 to your computer and use it in GitHub Desktop.
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