forked from keithio/angular-mailchimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-mailchimp.js
80 lines (67 loc) · 2.4 KB
/
angular-mailchimp.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* angular-mailchimp
* http://github.com/keithio/angular-mailchimp
* License: MIT
*/
'use strict';
angular.module('mailchimp', ['ng', 'ngResource', 'ngSanitize'])
/**
* Form controller for a new Mailchimp subscription.
*/
.controller('MailchimpSubscriptionCtrl', ['$log', '$resource', '$scope', '$rootScope',
function ($log, $resource, $scope, $rootScope) {
// Handle clicks on the form submission.
$scope.addSubscription = function (mailchimp) {
var actions,
MailChimpSubscription,
params = {},
url;
// Create a resource for interacting with the MailChimp API
url = '//' + mailchimp.username + '.' + mailchimp.dc +
'.list-manage.com/subscribe/post-json';
var fields = Object.keys(mailchimp);
for(var i = 0; i < fields.length; i++) {
params[fields[i]] = mailchimp[fields[i]];
}
params.c = 'JSON_CALLBACK';
actions = {
'save': {
method: 'jsonp'
}
};
MailChimpSubscription = $resource(url, params, actions);
// Send subscriber data to MailChimp
MailChimpSubscription.save(
// Successfully sent data to MailChimp.
function (response) {
// Define message containers.
mailchimp.errorMessage = '';
mailchimp.successMessage = '';
// Store the result from MailChimp
mailchimp.result = response.result;
// Mailchimp returned an error.
if (response.result === 'error') {
if (response.msg) {
// Remove error numbers, if any.
var errorMessageParts = response.msg.split(' - ');
if (errorMessageParts.length > 1)
errorMessageParts.shift(); // Remove the error number
mailchimp.errorMessage = errorMessageParts.join(' ');
} else {
mailchimp.errorMessage = 'Sorry! An unknown error occured.';
}
}
// MailChimp returns a success.
else if (response.result === 'success') {
mailchimp.successMessage = response.msg;
}
//Broadcast the result for global msgs
$rootScope.$broadcast('mailchimp-response', response.result, response.msg);
},
// Error sending data to MailChimp
function (error) {
$log.error('MailChimp Error: %o', error);
}
);
};
}]);