Created
September 11, 2011 21:53
-
-
Save groner/1210180 to your computer and use it in GitHub Desktop.
Quick and dirty AngularJS service wrapping location.replace()
This file contains hidden or 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
/* In place redirect using location.replace() | |
* | |
* Call redirect.redirect() with same arguments as $location.update() | |
* Call redirect.redirectHash() with same arguments as $location.updateHash() | |
* | |
* $route.otherwise({redirectTo: function() { | |
* return redirect.redirectHash(...) }}); | |
* | |
* Disuse after Vojta's $location replace is merged (0.10.1+). | |
*/ | |
angular.service('redirect', function() { | |
var $safe_location = angular.injector(this.$new(), | |
angular.extend({}, angular.service, { | |
$browser: extend_browser(angular.service.$browser), | |
}))('$location'); | |
return { | |
redirect: redirect, | |
redirectHash: redirectHash, | |
}; | |
function redirect(href) { | |
$safe_location.update(href); | |
location.replace($safe_location.href); | |
return $safe_location.href; | |
} | |
function redirectHash(path, search) { | |
$safe_location.updateHash(path, search); | |
location.replace($safe_location.href); | |
return $safe_location.hash; | |
} | |
// Derive a $browser service where setUrl is noop, this makes $location safe | |
// to use as a url calculator. | |
function extend_browser(original) { | |
extended.$inject = original.$inject.splice(); | |
function extended() { | |
X.prototype = original.apply(this, arguments); | |
function X() { this.setUrl = angular.noop; } | |
return new X; | |
} | |
return extended; | |
}; | |
}, {$inject: []}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment