-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartFactory.js
executable file
·44 lines (34 loc) · 1.33 KB
/
cartFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
angular.module('isCheckout').factory('cartFactory', ['$http', '$q','isCartConstants', function($http, $q,isCartConstants) {
return {
communicator: function(formDetails, cmethod, curl, contentType) {
/*
"deffered" as a variable it will first go see if that variable exists, if it does, it will use "deffered",
if not it will create an $q.defer() object and assign it to "deffered".
*/
var deffered = deffered || $q.defer();
/* Example Data, which can be sent from controller
formDetails = {formName: $scope.formName, formData: $scope.data};
*/
if(cmethod === 'POST'){
// Set the Content-Type
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}
var req = {
method: cmethod,
url: curl,
data: formDetails
};
$http(req).then(function(response) {
//On success callback
deffered.resolve(JSON.stringify(response.data));
}, function(errResponse) {
/*
On Error callback, get the error object in controller and assign it to an errObject globally.
And use the global errObject in template to assign to an element.
*/
deffered.reject(errResponse);
});
return deffered.promise;
}
};
}]);