Make $scope.project into a promise in the detail view

Using a promise here allows us to use the id from $scope.project when
browsing for the stories in the project, rather than parsing it from
the $stateParams. This enables us to use non-numeric ids without having
to make a GET to find the numeric ID twice (once in each controller).

Change-Id: If80a3e423a700b1105a7e48d65eefb21ecef700f
This commit is contained in:
Adam Coldrick 2018-02-27 11:58:41 +00:00 committed by Zara
parent 89b60c35ef
commit 52cbe7c07b
2 changed files with 14 additions and 29 deletions

View File

@ -37,13 +37,6 @@ angular.module('sb.projects').controller('ProjectDetailController',
parseInt($stateParams.id, 10) : parseInt($stateParams.id, 10) :
null; null;
/**
* The project we're manipulating right now.
*
* @type Project
*/
$scope.project = {};
/** /**
* UI flag for when we're initially loading the view. * UI flag for when we're initially loading the view.
* *
@ -101,19 +94,23 @@ angular.module('sb.projects').controller('ProjectDetailController',
* Load the project * Load the project
*/ */
function loadProject() { function loadProject() {
Project.get( return Project.get(
{'id': id}, {'id': id},
function (result) { function (result) {
// We've got a result, assign it to the view and unset our
// loading flag.
$scope.project = result;
handleServiceSuccess(); handleServiceSuccess();
return result;
}, },
handleServiceError handleServiceError
); );
} }
/**
* The project we're manipulating right now.
*
* @type Project
*/
$scope.project = loadProject();
/** /**
* Toggles the form back. * Toggles the form back.
*/ */
@ -159,6 +156,4 @@ angular.module('sb.projects').controller('ProjectDetailController',
handleServiceError handleServiceError
); );
}; };
loadProject();
}); });

View File

@ -22,18 +22,6 @@ angular.module('sb.projects').controller('ProjectStoryListController',
Preference, SubscriptionList, CurrentUser) { Preference, SubscriptionList, CurrentUser) {
'use strict'; 'use strict';
// Parse the ID. Since we're in a nested state, we don't really need
// to sanity check here, but in case of a future refactor we'll
// go ahead and do so anyway.
var id = $stateParams.hasOwnProperty('id') ?
parseInt($stateParams.id, 10) :
null;
if (id === null) {
$state.go('sb.index');
return;
}
var pageSize = Preference.get('project_detail_page_size'); var pageSize = Preference.get('project_detail_page_size');
// Variables and methods available to the template... // Variables and methods available to the template...
@ -65,7 +53,7 @@ angular.module('sb.projects').controller('ProjectStoryListController',
// Execute the story query. // Execute the story query.
Story.browse({ Story.browse({
project_id: id, project_id: $scope.project.id,
status: $scope.filter || null, status: $scope.filter || null,
offset: $scope.searchOffset, offset: $scope.searchOffset,
limit: pageSize, limit: pageSize,
@ -123,7 +111,7 @@ angular.module('sb.projects').controller('ProjectStoryListController',
}; };
$scope.newStory = function () { $scope.newStory = function () {
NewStoryService.showNewStoryModal(id) NewStoryService.showNewStoryModal($scope.project.id)
.then(function (story) { .then(function (story) {
// On success, go to the story detail // On success, go to the story detail
$state.go('sb.story.detail', {storyId: story.id}); $state.go('sb.story.detail', {storyId: story.id});
@ -133,7 +121,9 @@ angular.module('sb.projects').controller('ProjectStoryListController',
// Initialize the view with a default search. // Initialize the view with a default search.
resetScope(); resetScope();
$scope.project.$promise.then(function() {
$scope.search(); $scope.search();
});
// GET list of story subscriptions // GET list of story subscriptions
var cuPromise = CurrentUser.resolve(); var cuPromise = CurrentUser.resolve();