diff --git a/js/components/contactList/contactList_controller.js b/js/components/contactList/contactList_controller.js index d3358372c..8441a74b7 100644 --- a/js/components/contactList/contactList_controller.js +++ b/js/components/contactList/contactList_controller.js @@ -66,16 +66,32 @@ angular.module('contactsApp') }); }); + // Get contacts ContactService.getAll().then(function(contacts) { $scope.$apply(function() { ctrl.contacts = contacts; - if (!_.isEmpty(ctrl.contacts)) { - ctrl.setSelectedId(_.sortBy(contacts, function(contact) { - return contact.fullName(); - })[0].uid()); + }); + }); + + // Wait for ctrl.contactList to be updated, load the first contact and kill the watch + var unbindListWatch = $scope.$watch('ctrl.contactList', function() { + if(ctrl.contactList && ctrl.contactList.length > 0) { + // Check if a specific uid is requested + if($routeParams.uid) { + ctrl.contactList.forEach(function(contact) { + if(contact.uid() === $routeParams.uid) { + ctrl.setSelectedId($routeParams.uid); + ctrl.loading = false; + } + }); + } + // No contact previously loaded, let's load the first of the list + if(ctrl.loading) { + ctrl.setSelectedId(ctrl.contactList[0].uid()); } ctrl.loading = false; - }); + unbindListWatch(); + } }); $scope.$watch('ctrl.routeParams.uid', function(newValue) { @@ -111,11 +127,6 @@ angular.module('contactsApp') gid: $routeParams.gid, uid: ctrl.contactList[0].uid() }); - } else { - $route.updateParams({ - gid: $routeParams.gid, - uid: undefined - }); } unbindWatch(); // unbind as we only want one update });