Skip to content

Commit 9652327

Browse files
committed
Add $stateConfig provider
angular-ui/ui-router#265
1 parent b1deca6 commit 9652327

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

app/app.route.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
.run(services)
88
.run(redirection);
99

10-
routing.$inject = ['$stateProvider', '$urlRouterProvider'];
10+
routing.$inject = ['$stateConfigProvider', '$urlRouterProvider'];
1111

12-
function routing( $stateProvider, $urlRouterProvider ) {
12+
function routing( $stateConfigProvider, $urlRouterProvider ) {
1313

1414
// default route
1515
$urlRouterProvider.otherwise('/');
1616

1717
// decorator: set reasonable defaults for entity.* routes
18-
$stateProvider.decorator('views', function( state, parent ) {
18+
$stateConfigProvider.decorator('views', function( state, parent ) {
1919

2020
var config = state.self;
2121

@@ -34,7 +34,7 @@
3434
});
3535

3636
// app routes
37-
$stateProvider
37+
$stateConfigProvider
3838
.state('root', {
3939
url: '/',
4040
redirectTo: {

app/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@
4545
<script src="services/data.factory.js"></script>
4646
<script src="services/mapper.service.js"></script>
4747

48-
<!-- Models -->
48+
<!-- Custom Models -->
4949
<script src="models/artwork.service.js"></script>
5050
<script src="models/agent.service.js"></script>
5151

52+
<!-- Generic Models -->
53+
<script src="services/state-config.provider.js"></script>
54+
5255
<!-- States -->
5356
<script src="states/entity/entity.js"></script>
5457
<script src="states/entity/default.js"></script>

app/services/state-config.provider.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(function () {
2+
'use strict';
3+
4+
angular
5+
.module('app')
6+
.provider('$stateConfig', Provider);
7+
8+
// $state.get() allows us to check if a state exists,
9+
// but there's no equivallent method for $stateProvider.
10+
// https://github.com/angular-ui/ui-router/issues/265
11+
12+
// $stateConfigProvider stores the state's name,
13+
// before deferring calls to $stateProvider.
14+
15+
// Use $stateConfigProvider.exists() to check if a state exists
16+
17+
Provider.$inject = ['$stateProvider'];
18+
19+
function Provider( $stateProvider ) {
20+
21+
var states = [];
22+
23+
this.state = state;
24+
this.decorator = $stateProvider.decorator;
25+
26+
this.exists = exists;
27+
28+
this.$get = function() {
29+
return this;
30+
};
31+
32+
return this;
33+
34+
function state( name, data ) {
35+
36+
states.push( name );
37+
38+
$stateProvider.state.apply( this, arguments );
39+
40+
return this;
41+
42+
}
43+
44+
function exists( name ) {
45+
46+
return states.indexOf( name ) > -1;
47+
48+
}
49+
50+
}
51+
52+
})();

0 commit comments

Comments
 (0)