Refactoring permalink checker to handle multiple requests elegantly

This commit is contained in:
Rob Harrington
2015-01-20 08:24:43 +11:00
parent 7ad9fdf0be
commit 83726eba63
2 changed files with 50 additions and 19 deletions

View File

@@ -1,17 +1,23 @@
angular.module("admin.enterprises")
.controller "permalinkCtrl", ($scope, PermalinkChecker) ->
$scope.pristinePermalink = $scope.Enterprise.permalink
# locals
initialPermalink = $scope.Enterprise.permalink
pendingRequest = null
# variables on $scope
$scope.availablility = ""
$scope.checking = false
$scope.$watch "Enterprise.permalink", (newValue, oldValue) ->
if newValue == $scope.pristinePermalink
$scope.availability = ""
else
$scope.checking = true
PermalinkChecker.check(newValue).then (data) ->
$scope.availability = 'Available'
$scope.checking = false
, (data) ->
$scope.availability = 'Unavailable'
$scope.checking = false
$scope.checking = true
pendingRequest = PermalinkChecker.check(newValue)
pendingRequest.then (data) ->
if data.permalink == initialPermalink
$scope.availability = ""
else
$scope.availability = data.available
$scope.Enterprise.permalink = data.permalink
$scope.checking = false
, (data) ->
# Do nothing (this is hopefully an aborted request)