Skip to content

Instantly share code, notes, and snippets.

@stephen-james
Created March 17, 2015 10:26
Show Gist options
  • Save stephen-james/104bb97843ddb1c4bcd4 to your computer and use it in GitHub Desktop.
Save stephen-james/104bb97843ddb1c4bcd4 to your computer and use it in GitHub Desktop.
App host notifier, required by apps to handle development/debug notifications to and from the app host
angular
.module('ampAppHostNotifier', [])
.factory('appHostNotifier', [
'$rootScope',
'$window',
'$location',
function($rootScope, $window, $location) {
return {
init: function() {
if ($window.parent !== $window) {
$rootScope.$on("$routeChangeSuccess", function(e, toRoute) {
var appInfo = { url: $location.url() };
var route = toRoute.$$route;
appInfo.route = {
controller: route.controller,
controllerAs: route.controllerAs,
template: route.template,
templateUrl: route.templateUrl
};
appInfo.params = _.extend({}, toRoute.params, toRoute.pathParams);
$window.parent.postMessage(appInfo, 'http://0.0.0.0:9090');
});
$rootScope.$on("$stateChangeSuccess", function(e, toState, toParams) {
var appInfo = { url: $location.url() };
var stateInfo = angular.copy(toState);
delete stateInfo.resolve;
appInfo.state = stateInfo;
appInfo.params = toParams;
$window.parent.postMessage(appInfo, 'http://0.0.0.0:9090');
});
function parseAppMessage(event)
{
if (event.data === 'reload') {
$window.location.reload();
}
}
$window.addEventListener("message", parseAppMessage, false);
}
}
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment