forked from zendesk/demo_apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (124 loc) · 3.6 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(function() {
// Hook reference: http://developer.zendesk.com/documentation/apps/reference/hooks.html
return {
defaultState: 'showCase',
requests: {
heartQuote: {
url: 'http://iheartquotes.com/api/v1/random',
data: {
format: 'json'
}
}
},
events: {
'app.activated': 'init',
'ticket.save': 'hookHandler',
// Switch to different hook handlers
'click .simple_pass': 'useSimplePass', // pass
'click .simple_fail': 'useSimpleFail', // fail
'click .string_fail': 'useStringFail', // fail with an error message
'click .delayed_pass': 'useDelayedPass', // pass 3s after clicking submit
'click .delayed_fail': 'useDelayedFail', // fail 3s after clicking submit
'click .ajax_pass': 'useAjaxPass', // pass after getting a quote through ajax
'click .ajax_fail': 'useAjaxFail' // fail after getting a quote through ajax
},
init: function() {
this.currentHandler = this.useSimplePass;
},
hookHandler: function() {
return this.currentHandler();
},
// Switches that control which handler is used to handle `ticket.save`
useSimplePass: function(e) {
this.currentHandler = this.simplePass;
console.log('Using simple pass');
this.toggleActive(e);
},
useSimpleFail: function(e) {
this.currentHandler = this.simpleFail;
console.log('Using simple fail');
this.toggleActive(e);
},
useStringFail: function(e) {
this.currentHandler = this.stringFail;
console.log('Using string fail');
this.toggleActive(e);
},
useDelayedPass: function(e) {
this.currentHandler = this.delayedPass;
console.log('Using delayed pass');
this.toggleActive(e);
},
useDelayedFail: function(e) {
this.currentHandler = this.delayedFail;
console.log('Using delayed fail');
this.toggleActive(e);
},
useAjaxPass: function(e) {
this.currentHandler = this.ajaxPass;
console.log('Using ajax pass');
this.toggleActive(e);
},
useAjaxFail: function(e) {
this.currentHandler = this.ajaxFail;
console.log('Using ajax fail');
this.toggleActive(e);
},
// Handles
simplePass: function() {
return true;
},
simpleFail: function() {
return false;
},
stringFail: function() {
return this.I18n.t('fail_string');
},
delayedPass: function() {
return this.promise(function(done, fail) {
setTimeout(function() {
done();
}, 3000);
});
},
delayedFail: function() {
return this.promise(function(done, fail) {
setTimeout(function() {
fail();
}, 3000);
});
},
ajaxPass: function() {
return this.promise(function(done, fail) {
this.ajax('heartQuote').then(
function(data) {
console.log(data.quote);
done();
},
function() {
console.log('ajax failed, but ticket.save shall pass');
done();
}
);
});
},
ajaxFail: function() {
return this.promise(function(done, fail) {
this.ajax('heartQuote').then(
function(data) {
console.log(data.quote);
fail();
},
function() {
console.log('ajax failed, ticket.save shall fail anyway');
fail();
}
);
});
},
toggleActive: function(e) {
this.$('.btn').removeClass('active');
this.$(e.currentTarget).addClass('active');
}
};
}());