File tree Expand file tree Collapse file tree 3 files changed +28
-14
lines changed Expand file tree Collapse file tree 3 files changed +28
-14
lines changed Original file line number Diff line number Diff line change 9
9
10
10
var vm = this ;
11
11
vm . title = 'FavouriteListController' ;
12
- vm . faves = favouriteService . faves ;
12
+ vm . faves = [ ] ;
13
+ vm . faveKeys = [ ] ;
13
14
vm . deleteFave = deleteFave ;
14
15
vm . navigate = navigate ;
15
16
19
20
20
21
function activate ( ) {
21
22
$log . debug ( vm . title + ' instantiated' ) ;
23
+ favouriteService . faves . $watch ( generateIndex ) ;
24
+ generateIndex ( ) ;
22
25
}
23
26
24
- function deleteFave ( fave ) {
25
- vm . faves . $remove ( fave ) ;
27
+ function generateIndex ( ) {
28
+ vm . faves = _ . groupBy ( favouriteService . faves , nameOrTitle ) ;
29
+ vm . faveKeys = _ . sortBy ( _ . keys ( vm . faves ) ) ;
30
+ //////////
31
+ function nameOrTitle ( fave ) {
32
+ return _ . first ( fave . name || fave . title ) ;
33
+ }
34
+ }
35
+
36
+ function deleteFave ( fave ) {
37
+ favouriteService . faves . $remove ( fave ) ;
26
38
}
27
39
28
- function navigate ( fave ) {
29
- if ( fave . type === 'character' ) {
30
- $state . go ( 'app.characterDetailInModal' , { character :fave } ) ;
40
+ function navigate ( fave ) {
41
+ if ( fave . type === 'character' ) {
42
+ $state . go ( 'app.characterDetailInModal' , { character : fave } ) ;
31
43
} else {
32
- $state . go ( 'app.comicDetailInModal' , { comic :fave } ) ;
44
+ $state . go ( 'app.comicDetailInModal' , { comic : fave } ) ;
33
45
}
34
46
}
35
47
}
Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ ion-view(view-title='Favourites')
2
2
ion-content
3
3
ion-nav-view( name ='app-favourites-detail' )
4
4
ion-list
5
- ion-item.item-avatar ( ng-click ='vm.navigate(fave)' ng-repeat ="fave in vm.faves | orderBy:'name || title' track by fave.id" )
5
+ div.item.item-divider ( ng-repeat-start ="letter in vm.faveKeys" ng-bind ='letter' )
6
+ ion-item.item-avatar ( ng-click ='vm.navigate(fave)' ng-repeat ="fave in vm.faves[letter] | orderBy:'name || title' track by fave.id" )
6
7
img( img-cache ic-src ='{{::fave.thumbnailUrl}}' )
7
- h2 {{ fave.name || fave.title}}
8
- p.fave__meta {{ ::fave.type}}
8
+ h2( ng-bind = ':: fave.name || fave.title' )
9
+ p.fave__meta ( ng-bind = ' ::fave.type' )
9
10
ion-option-button.button-assertive ( ng-click ='vm.deleteFave(fave)' ) Delete
11
+ div( ng-repeat-end )
Original file line number Diff line number Diff line change 27
27
function watch ( ) {
28
28
$log . info ( faves . length + ' items in favourite list' ) ;
29
29
// WARNING: when adding, I'm using angularfire because it handles a strange conceptual choice in Firebase which I don't like.
30
- // When listening to 'child_added' you are called once per item in the db corresponding to your ref.
30
+ // When listening to 'child_added' you are called once per item in the db corresponding to your ref.
31
31
// It is not just new events ; but also existing ones. You don't get to write boilerplate code to overcome this with angularfire.
32
32
faves . $watch ( function ( data ) {
33
33
$log . debug ( 'Firebase event:' , data ) ;
34
34
if ( data . event === 'child_added' )
35
- // Listeners will receive an actual record with $id and $priority keys.
35
+ // Listeners will receive an actual record with $id and $priority keys.
36
36
// If you just send raw values, you will lose the ability to remove it.
37
37
$rootScope . $emit ( 'fave:added' , faves . $getRecord ( data . key ) ) ;
38
38
} ) ;
39
39
// WARNING: when deleting, I'm using firebase directly because angularfire just give you the deleted key but not the actual
40
- // record. And you can't get it yourself because by the time you get notified, the local is already deleted.
40
+ // record. And you can't get it yourself because by the time you get notified, the local one is already deleted.
41
41
// So in order to get best of both world and do what I want to do the easy way, I'm mixing here angularfire and pure firebase
42
- // code.
42
+ // code.
43
43
var ref = faves . $ref ( ) ;
44
44
ref . on ( 'child_removed' , function ( snapshot ) {
45
45
var value = snapshot . val ( ) ;
You can’t perform that action at this time.
0 commit comments