refactor($location): merged $locationConfig service into $locationProvider

This commit is contained in:
Misko Hevery 2011-11-08 20:42:16 -08:00
parent b3c17f3fdc
commit c283bf6035
5 changed files with 38 additions and 34 deletions

View file

@ -88,26 +88,22 @@ setter methods that allow you to get or change the current URL in the browser.
## $location service configuration
To configure the `$location` service, you define the `$locationConfig` service which is an object
with configuration properties:
To configure the `$location` service, you get a hold of
{@link angular.module.ng.$locationProvider $locationProvider} service and configure it with these
methods:
- **html5Mode**: {boolean}<br />
- **html5Mode(mode)**: {boolean}<br />
`true` - see HTML5 mode<br />
`false` - see Hashbang mode<br />
default: `false`
- **hashPrefix**: {string}<br />
- **hashPrefix(prefix)**: {string}<br />
prefix used for Hashbang URLs (used in Hashbang mode or in legacy browser in Html5 mode)<br />
default: `'!'`
### Example configuration
<pre>
angular.service('$locationConfig', function() {
return {
html5mode: true,
hashPrefix: '!'
};
});
$locationProvider.html5Mode(true).hashPrefix('!');
</pre>
## Getter and setter methods

View file

@ -152,9 +152,6 @@ function TutorialInstructionsCtrl($cookieStore) {
window.angular = window.angular || {};
angular.module = angular.module || {};
angular.module.ngdocs = function($provide) {
$provide.value('$locationConfig', {
html5Mode: true,
hashPrefix: '!'
});
angular.module.ngdocs = function($locationProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');
};

View file

@ -954,7 +954,6 @@ function ngModule($provide, $injector) {
$provide.service('$filter', $FilterProvider);
$provide.service('$formFactory', $FormFactoryProvider);
$provide.service('$location', $LocationProvider);
$provide.service('$locationConfig', $LocationConfigProvider);
$provide.service('$log', $LogProvider);
$provide.service('$parse', $ParseProvider);
$provide.service('$resource', $ResourceProvider);

View file

@ -420,15 +420,35 @@ function locationGetterSetter(property, preprocess) {
* For more information see {@link guide/dev_guide.services.$location Developer Guide: Angular Services: Using $location}
*/
function $LocationProvider(){
this.$get = ['$rootScope', '$browser', '$sniffer', '$locationConfig', '$document',
function( $rootScope, $browser, $sniffer, $locationConfig, $document) {
var hashPrefix = '',
html5Mode = false;
this.hashPrefix = function(prefix) {
if (isDefined(prefix)) {
hashPrefix = prefix;
return this;
} else {
return html5Mode;
}
}
this.html5Mode = function(mode) {
if (isDefined(mode)) {
html5Mode = mode;
return this;
} else {
return html5Mode;
}
};
this.$get = ['$rootScope', '$browser', '$sniffer', '$document',
function( $rootScope, $browser, $sniffer, $document) {
var currentUrl,
basePath = $browser.baseHref() || '/',
pathPrefix = pathPrefixFromBase(basePath),
hashPrefix = $locationConfig.hashPrefix || '',
initUrl = $browser.url();
if ($locationConfig.html5Mode) {
if (html5Mode) {
if ($sniffer.history) {
currentUrl = new LocationUrl(convertToHtml5Url(initUrl, basePath, hashPrefix), pathPrefix);
} else {
@ -505,13 +525,3 @@ function $LocationProvider(){
return currentUrl;
}];
}
//TODO(misko): refactor to service
function $LocationConfigProvider(){
this.$get = function() {
return {
html5Mode: false,
hashPrefix: ''
};
};
}

View file

@ -308,8 +308,9 @@ describe('$location', function() {
function initService(html5Mode, hashPrefix, supportHistory) {
return function($provide){
$provide.value('$locationConfig', {html5Mode: html5Mode, hashPrefix: hashPrefix});
return function($provide, $locationProvider){
$locationProvider.html5Mode(html5Mode);
$locationProvider.hashPrefix(hashPrefix);
$provide.value('$sniffer', {history: supportHistory});
};
}
@ -576,7 +577,7 @@ describe('$location', function() {
var root, link, originalBrowser, lastEventPreventDefault;
function configureService(linkHref, html5Mode, supportHist, attrs, content) {
return function($provide){
return function($provide, $locationProvider){
var jqRoot = jqLite('<div></div>');
attrs = attrs ? ' ' + attrs + ' ' : '';
link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0];
@ -586,7 +587,8 @@ describe('$location', function() {
$provide.value('$document', jqRoot);
$provide.value('$sniffer', {history: supportHist});
$provide.value('$locationConfig', {html5Mode: html5Mode, hashPrefix: '!'});
$locationProvider.html5Mode(html5Mode);
$locationProvider.hashPrefix('!');
};
}