Wrap http requests in angular

This commit is contained in:
James Allen 2014-06-23 11:10:18 +01:00
parent a2663c0f32
commit 450ffd0564
4 changed files with 33 additions and 54 deletions

View file

@ -156,69 +156,39 @@ define [
failure: (error) -> callback(error)
}
createFolderInCurrentFolder: (name, callback = (error, doc) ->) ->
createFolderInCurrentFolder: (name) ->
# We'll wait for the socket.io notification to actually
# add the folder for us.
parent_folder = @getCurrentFolder()
$.ajax {
url: "/project/#{@ide.project_id}/folder"
type: "POST"
contentType: "application/json; charset=utf-8"
data: JSON.stringify {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
dataType: "json"
success: (folder) ->
callback(null, folder)
failure: (error) -> callback(error)
return @ide.$http.post "/project/#{@ide.project_id}/folder", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
renameEntity: (entity, name, callback = (error) ->) ->
return if entity.name == name
entity.name = name
$.ajax {
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/rename"
type: "POST"
contentType: "application/json; charset=utf-8"
data: JSON.stringify {
name: name,
_csrf: window.csrfToken
}
dataType: "json"
success: () -> callback()
failure: (error) -> callback(error)
return @ide.$http.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/rename", {
name: name,
_csrf: window.csrfToken
}
deleteEntity: (entity, callback = (error) ->) ->
# We'll wait for the socket.io notification to
# delete from scope.
$.ajax {
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}"
type: "DELETE"
contentType: "application/json; charset=utf-8"
return @ide.$http {
method: "DELETE"
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}"
headers:
"X-Csrf-Token": window.csrfToken
dataType: "json"
success: () -> callback()
failure: (error) -> callback(error)
}
moveEntity: (entity, parent_folder, callback = (error) ->) ->
@_moveEntityInScope(entity, parent_folder)
$.ajax {
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/move"
type: "POST"
contentType: "application/json; charset=utf-8"
headers:
"X-Csrf-Token": window.csrfToken
data: JSON.stringify {
folder_id: parent_folder.id
}
dataType: "json"
success: () -> callback()
failure: (error) -> callback(error)
return @ide.$http.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/move", {
folder_id: parent_folder.id
_csrf: window.csrfToken
}
_deleteEntityFromScope: (entity) ->

View file

@ -47,9 +47,11 @@ define [
$scope.create = () ->
$scope.state.inflight = true
ide.fileTreeManager.createDocInCurrentFolder $scope.inputs.name, (error, doc) ->
$scope.state.inflight = false
$modalInstance.close()
ide.fileTreeManager
.createDocInCurrentFolder($scope.inputs.name)
.success () ->
$scope.state.inflight = false
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss('cancel')
@ -70,9 +72,11 @@ define [
$scope.create = () ->
$scope.state.inflight = true
ide.fileTreeManager.createFolderInCurrentFolder $scope.inputs.name, (error, doc) ->
$scope.state.inflight = false
$modalInstance.close()
ide.fileTreeManager
.createFolderInCurrentFolder($scope.inputs.name)
.success () ->
$scope.state.inflight = false
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss('cancel')

View file

@ -39,8 +39,9 @@ define [
$scope.delete = () ->
$scope.state.inflight = true
ide.fileTreeManager.deleteEntity $scope.entity, (error) ->
$scope.$apply () ->
ide.fileTreeManager
.deleteEntity($scope.entity)
.success () ->
$scope.state.inflight = false
$modalInstance.close()

View file

@ -3,5 +3,9 @@ define [
], (App) ->
# We create and provide this as service so that we can access the global ide
# from within other parts of the angular app.
App.factory "ide", () ->
return {}
App.factory "ide", ["$http", ($http) ->
ide = {}
ide.$http = $http
return ide
]