Skip to content

Commit 5e8832d

Browse files
committed
Add decorator for entity state defaults
1 parent bf50ff8 commit 5e8832d

File tree

6 files changed

+78
-60
lines changed

6 files changed

+78
-60
lines changed

app/app.route.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
// default route
1515
$urlRouterProvider.otherwise('/');
1616

17+
// decorator: set reasonable defaults for entity.* routes
18+
$stateProvider.decorator('views', function( state, parent ) {
19+
20+
var config = state.self;
21+
22+
// console.log( config );
23+
24+
if( config.name.match(/^entity\./) ) {
25+
26+
config.templateUrl = config.templateUrl || 'states/entity/default.html';
27+
config.controller = config.controller || 'DefaultEntityController';
28+
config.controllerAs = 'vm';
29+
30+
}
31+
32+
return parent(state);
33+
34+
});
35+
1736
// app routes
1837
$stateProvider
1938
.state('root', {
@@ -36,12 +55,8 @@
3655
})
3756
.state('entity.artwork', {
3857
url: '/artworks/:id',
39-
// TODO: Create custom templates for each?
40-
templateUrl: 'states/entity/default.html',
41-
controller: 'ArtworkController',
42-
controllerAs: 'vm',
43-
data: {
44-
cssClassnames: 'aic-state-artwork'
58+
params: {
59+
model: 'ArtworkService'
4560
}
4661
});
4762

app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<!-- States -->
5252
<script src="states/entity/entity.js"></script>
53-
<script src="states/entity/artwork/artwork.js"></script>
53+
<script src="states/entity/default.js"></script>
5454

5555
<!-- Load the app itself last -->
5656
<script src="app.route.js"></script>

app/states/entity/artwork/artwork.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/states/entity/default.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(function () {
2+
3+
angular
4+
.module('app')
5+
.controller('DefaultEntityController', Controller);
6+
7+
Controller.$inject = ['$stateParams', '$injector'];
8+
9+
function Controller( $stateParams, $injector ) {
10+
11+
var ModelService = $injector.get( $stateParams.model );
12+
13+
var vm = this;
14+
15+
vm.entity = null;
16+
vm.route = null;
17+
18+
activate();
19+
20+
return vm;
21+
22+
function activate() {
23+
24+
if( !$stateParams.id ) {
25+
26+
// Just for testing: if id is omitted, get first one in the list
27+
ModelService.list( { limit: 1 } ).promise.then( function() {
28+
29+
vm.entity = ModelService.list( { limit: 1 } ).cache.clean[0];
30+
vm.route = ModelService.route( vm.entity.id );
31+
32+
});
33+
34+
} else {
35+
36+
vm.entity = ModelService.detail( $stateParams.id ).cache.clean;
37+
vm.route = ModelService.route( $stateParams.id );
38+
39+
}
40+
41+
42+
43+
}
44+
45+
}
46+
47+
})();

app/states/entity/entity.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<div class="aic-view-entity" ng-submit="vm.open( vm.form.state, vm.form.id )">
1+
<div class="aic-view-entity" ng-submit="vm.open( vm.form.model, vm.form.id )">
22

33
<form class="form-horizontal">
44

55
<div class="input-group">
66

7-
<select class="form-control" ng-model="vm.form.state">
7+
<select class="form-control" ng-model="vm.form.model">
88

99
<option ng-repeat="model in vm.models track by $index"
10-
value="{{model.states.entity}}">{{model.name}}</option>
10+
ng-value="model">{{model.name}}</option>
1111

1212
</select>
1313

app/states/entity/entity.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@
3131
];
3232

3333
vm.form = {
34-
state: vm.models[0].states.entity,
34+
model: vm.models[0],
3535
id: null,
3636
};
3737

3838
}
3939

40-
function open( state, id ) {
40+
function open( model, id ) {
4141

42-
console.log( arguments );
43-
44-
$state.go( state, { id: id } );
42+
$state.go( model.states.entity, {
43+
// model is optional since it has a default
44+
id: id,
45+
});
4546

4647
}
4748

0 commit comments

Comments
 (0)