Skip to content

Commit

Permalink
feature/bookmark-list — Big heavy work to get dividers in favourite l…
Browse files Browse the repository at this point in the history
…ist. Not sure it is worth it.
  • Loading branch information
jeandat committed May 2, 2016
1 parent 485974d commit 22a2544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
26 changes: 19 additions & 7 deletions app/favourite/favourite-list.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

var vm = this;
vm.title = 'FavouriteListController';
vm.faves = favouriteService.faves;
vm.faves = [];
vm.faveKeys = [];
vm.deleteFave = deleteFave;
vm.navigate = navigate;

Expand All @@ -19,17 +20,28 @@

function activate() {
$log.debug(vm.title + ' instantiated');
favouriteService.faves.$watch(generateIndex);
generateIndex();
}

function deleteFave(fave){
vm.faves.$remove(fave);
function generateIndex(){
vm.faves = _.groupBy(favouriteService.faves, nameOrTitle);
vm.faveKeys = _.sortBy(_.keys(vm.faves));
//////////
function nameOrTitle(fave) {
return _.first(fave.name || fave.title);
}
}

function deleteFave(fave) {
favouriteService.faves.$remove(fave);
}

function navigate(fave){
if(fave.type === 'character'){
$state.go('app.characterDetailInModal', {character:fave});
function navigate(fave) {
if (fave.type === 'character') {
$state.go('app.characterDetailInModal', {character: fave});
} else {
$state.go('app.comicDetailInModal', {comic:fave});
$state.go('app.comicDetailInModal', {comic: fave});
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions app/favourite/favourite-list.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ ion-view(view-title='Favourites')
ion-content
ion-nav-view(name='app-favourites-detail')
ion-list
ion-item.item-avatar(ng-click='vm.navigate(fave)' ng-repeat="fave in vm.faves | orderBy:'name || title' track by fave.id")
div.item.item-divider(ng-repeat-start="letter in vm.faveKeys" ng-bind='letter')
ion-item.item-avatar(ng-click='vm.navigate(fave)' ng-repeat="fave in vm.faves[letter] | orderBy:'name || title' track by fave.id")
img(img-cache ic-src='{{::fave.thumbnailUrl}}')
h2 {{fave.name || fave.title}}
p.fave__meta {{::fave.type}}
h2(ng-bind='::fave.name || fave.title')
p.fave__meta(ng-bind='::fave.type')
ion-option-button.button-assertive(ng-click='vm.deleteFave(fave)') Delete
div(ng-repeat-end)
8 changes: 4 additions & 4 deletions app/favourite/favourite.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
function watch() {
$log.info(faves.length + ' items in favourite list');
// WARNING: when adding, I'm using angularfire because it handles a strange conceptual choice in Firebase which I don't like.
// When listening to 'child_added' you are called once per item in the db corresponding to your ref.
// When listening to 'child_added' you are called once per item in the db corresponding to your ref.
// It is not just new events ; but also existing ones. You don't get to write boilerplate code to overcome this with angularfire.
faves.$watch(function (data) {
$log.debug('Firebase event:', data);
if (data.event === 'child_added')
// Listeners will receive an actual record with $id and $priority keys.
// Listeners will receive an actual record with $id and $priority keys.
// If you just send raw values, you will lose the ability to remove it.
$rootScope.$emit('fave:added', faves.$getRecord(data.key));
});
// WARNING: when deleting, I'm using firebase directly because angularfire just give you the deleted key but not the actual
// record. And you can't get it yourself because by the time you get notified, the local is already deleted.
// record. And you can't get it yourself because by the time you get notified, the local one is already deleted.
// 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
// code.
// code.
var ref = faves.$ref();
ref.on('child_removed', function (snapshot) {
var value = snapshot.val();
Expand Down

0 comments on commit 22a2544

Please sign in to comment.