-
-
Save rePassante/5695949 to your computer and use it in GitHub Desktop.
AngularJS - Shared Data Services & Handling Page Refreshes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Check For Customer Data In sharedData --> | |
if (sharedData.getNewlyAddedCustomer() != null) { | |
<!-- The Data Exists In sharedData, add it to $scope and Proceed Normally --> | |
$scope.newlyAddedCustomerRequest = sharedData.getNewlyAddedCustomerRequest(); | |
} else { | |
<!-- The Data DOES NOT Exist In sharedData, Let's check the hash and see if we can re-fetch --> | |
var path = $location.path(); | |
var splitPath = path.split("/"); | |
<!-- When we eval our split path we can eval to see if we have info after the route | |
if (splitPath.length > 2) { | |
<!-- We have the info we need so we can re-fetch the customer details by the Id in the hash --> | |
$scope.responseFromServer = MockDataResource.get({ id: splitPath[2] }, | |
function (response) { | |
//Success - Re-Add To sharedData & $scope | |
sharedData.setNewlyAddedCustomerRequest(response); | |
$scope.newlyAddedCustomerRequest = sharedData.getNewlyAddedCustomerRequest(); | |
}, | |
function (error) { | |
//Error | |
console.error("Unable to fetch Customer Details"); | |
} | |
); | |
} else { | |
<!-- The required info isn't present in the hash, let's go back to our customers page gracefully --> | |
$location.path('/customer'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment