The Exercise
resource defined in WorkoutService
already has the save
and update
(custom actions that we added) action. It's now just a matter of invoking the correct action inside the WorkoutBuilderService
functions.
The first ExerciseBuilderService
function we fix is save
. Update the save
implementation with the following code:
service.save = function () { if (!buildingExercise._id) buildingExercise._id = buildingExercise.name; var promise = newExercise ? WorkoutService.Exercises.save({},buildingExercise).$promise : buildingExercise.$update({ id: buildingExercise.name }); return promise.then(function (data) { newExercise = false; return buildingExercise; }); };
In the previous implementation based on the newExercise
state, we call the appropriate resource action. We then pull out the underlying promise and again perform promise chaining to return the same exercise in future using then
.
The save
operation not only uses...