Skip to content

Commit 22a2544

Browse files
committed
feature/bookmark-list — Big heavy work to get dividers in favourite list. Not sure it is worth it.
1 parent 485974d commit 22a2544

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

app/favourite/favourite-list.controller.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
var vm = this;
1111
vm.title = 'FavouriteListController';
12-
vm.faves = favouriteService.faves;
12+
vm.faves = [];
13+
vm.faveKeys = [];
1314
vm.deleteFave = deleteFave;
1415
vm.navigate = navigate;
1516

@@ -19,17 +20,28 @@
1920

2021
function activate() {
2122
$log.debug(vm.title + ' instantiated');
23+
favouriteService.faves.$watch(generateIndex);
24+
generateIndex();
2225
}
2326

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);
2638
}
2739

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});
3143
} else {
32-
$state.go('app.comicDetailInModal', {comic:fave});
44+
$state.go('app.comicDetailInModal', {comic: fave});
3345
}
3446
}
3547
}

app/favourite/favourite-list.jade

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ ion-view(view-title='Favourites')
22
ion-content
33
ion-nav-view(name='app-favourites-detail')
44
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")
67
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')
910
ion-option-button.button-assertive(ng-click='vm.deleteFave(fave)') Delete
11+
div(ng-repeat-end)

app/favourite/favourite.factory.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727
function watch() {
2828
$log.info(faves.length + ' items in favourite list');
2929
// 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.
3131
// It is not just new events ; but also existing ones. You don't get to write boilerplate code to overcome this with angularfire.
3232
faves.$watch(function (data) {
3333
$log.debug('Firebase event:', data);
3434
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.
3636
// If you just send raw values, you will lose the ability to remove it.
3737
$rootScope.$emit('fave:added', faves.$getRecord(data.key));
3838
});
3939
// 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.
4141
// 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.
4343
var ref = faves.$ref();
4444
ref.on('child_removed', function (snapshot) {
4545
var value = snapshot.val();

0 commit comments

Comments
 (0)