Created
February 13, 2016 16:53
-
-
Save jlcarvalho/8017850c90e83a22fe65 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
/** | |
* Created by JeanLucas on 26/07/2015. | |
*/ | |
function run ($rootScope, $location, $window) { | |
var parentDomain = 'http://localhost:3000'; | |
// A function to process messages received by the window. | |
function receiveMessage(e) { | |
// Check to make sure that this message came from the correct domain. | |
if (e.origin !== parentDomain) return; | |
if (e.data.key === 'route' && angular.isDefined(e.data.value)) setRoute(e.data.value); | |
} | |
function setRoute (route) { | |
$location.path( route.charAt(0) === '#' ? route.slice(1, route.length) : route ); | |
$rootScope.$digest(); | |
console.log( $location.path() ); | |
} | |
$rootScope.$on('$stateChangeSuccess', function(event, toState) { | |
$window.parent.postMessage({ type: 'route', message: toState.url } , parentDomain) | |
}); | |
// Setup an event listener that calls receiveMessage() when the window | |
// receives a new MessageEvent. | |
let eventMethod = $window.addEventListener ? "addEventListener" : "attachEvent"; | |
let eventer = $window[eventMethod]; | |
let messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; | |
eventer(messageEvent, receiveMessage); | |
} | |
run.$inject = ['$rootScope', '$location', '$window']; | |
export default run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment