Skip to content

Instantly share code, notes, and snippets.

@rePassante
Created June 3, 2013 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rePassante/5695949 to your computer and use it in GitHub Desktop.
Save rePassante/5695949 to your computer and use it in GitHub Desktop.
AngularJS - Shared Data Services & Handling Page Refreshes
<!-- 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