Created
May 20, 2013 01:41
-
-
Save boycaught/5609923 to your computer and use it in GitHub Desktop.
AngularJS HTTP call skeleton, for posting to CouchDB (personal tutorial)
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
function CreateStoryController($scope, $http) { | |
// Create callback for success, injected into controller scope. | |
$scope.submitStory = function() { | |
var success = function(data, status, headers, config) { | |
$scope.message = 'Success!'; | |
}; | |
// Create callback for failure. | |
var failure = function(data, status, headers, config) { | |
$scope.message = 'Problem'; | |
}; | |
// Define callback to execute the createStory() action over http. | |
var submitStory = function (storydata, success, failure) { | |
var payload = $.param(storydata); | |
var request = { | |
method: 'POST', | |
url: '/couchdb/database/', | |
data: payload, | |
headers: {'Content-Type': 'application/json'} | |
}; | |
$http(request) | |
.success(success) | |
.error(failure); | |
} | |
// Inject the payload into the scope. | |
// Execute the submitStory() function. | |
submitStory($scope.storydata, success, failure); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment