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

View File

@ -22,18 +22,6 @@ angular.module('sb.projects').controller('ProjectStoryListController',
Preference, SubscriptionList, CurrentUser) {
'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');
// Variables and methods available to the template...
@ -65,7 +53,7 @@ angular.module('sb.projects').controller('ProjectStoryListController',
// Execute the story query.
Story.browse({
project_id: id,
project_id: $scope.project.id,
status: $scope.filter || null,
offset: $scope.searchOffset,
limit: pageSize,
@ -123,7 +111,7 @@ angular.module('sb.projects').controller('ProjectStoryListController',
};
$scope.newStory = function () {
NewStoryService.showNewStoryModal(id)
NewStoryService.showNewStoryModal($scope.project.id)
.then(function (story) {
// On success, go to the story detail
$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.
resetScope();
$scope.search();
$scope.project.$promise.then(function() {
$scope.search();
});
// GET list of story subscriptions
var cuPromise = CurrentUser.resolve();