-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONRequest.coffee
More file actions
81 lines (64 loc) · 2.69 KB
/
Copy pathJSONRequest.coffee
File metadata and controls
81 lines (64 loc) · 2.69 KB
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
# JSONRequest created by Jordan Dobson on 17 Nov 2016 - @jordandobson - jordandobson@gmail.com
class exports.JSONRequest extends Framer.BaseClass
# https://davidwalsh.name/xmlhttprequest
DATA = "data"
EVENT = "event"
@LOAD = LOAD = "load"
@ABORT = ABORT = "abort"
@ERROR = ERROR = "error"
@PROGRESS = PROGRESS = "progress"
Events.JSONLoaded = "JSONRequest.#{LOAD }"
Events.JSONAbort = "JSONRequest.#{ABORT }"
Events.JSONError = "JSONRequest.#{ERROR }"
Events.JSONProgress = "JSONRequest.#{PROGRESS}"
Events.JSONEvent = "JSONRequest.#{EVENT }"
MSG_LOAD_ERROR = "Data didn't load"
MSG_LOAD_ABORT = "Loading Aborted"
@define DATA, get: -> @_getPropertyValue DATA
constructor: (options={}) ->
@request = @url = @response = @event = null
super options
@url = options.url
@request = new XMLHttpRequest()
setupEvents @
# Public Methods #####################################################
get: (url) ->
@request.open 'GET', url or @url, true
@request.send null
# Private Functions ##################################################
setupEvents = (self) ->
self.request.addEventListener LOAD, ((e) -> handleResponse self, e), false
self.request.addEventListener PROGRESS, ((e) -> handleResponse self, e), false
self.request.addEventListener ERROR, ((e) -> handleResponse self, e), false
self.request.addEventListener ABORT, ((e) -> handleResponse self, e), false
handleResponse = (self, event) ->
switch event.type
when LOAD then handleData self, event
when ERROR then handleException self, MSG_LOAD_ERROR, ERROR
when ABORT then handleException self, MSG_LOAD_ABORT, ABORT
when PROGRESS then handleProgress self
handleData = (self, event) ->
response = event.target.response
jsonParse = JSON.parse response
self.response = response
self._setPropertyValue DATA, jsonParse
self.emit Events.JSONLoaded, jsonParse
self.emit Events.JSONEvent, LOAD, jsonParse
handleProgress = (self) ->
self.emit Events.JSONProgress
self.emit Events.JSONEvent, PROGRESS
handleException = (self, message, type) ->
self.emit Events.JSONAbort if type is ABORT
self.emit Events.JSONError if type is ERROR
self.emit Events.JSONEvent, ERROR if type is ERROR
self.emit Events.JSONEvent, ABORT if type is ABORT
requestExceptionError ERROR, "JSONRequest from URL: #{self.url}" if type is ERROR
requestExceptionError ABORT, "JSONRequest aborted from URL: #{self.url}" if type is ABORT
# Fix Add Error Display
requestExceptionError = (type, message) ->
Framer.Extras.ErrorDisplay.enable()
if type is ERROR
console.error message
throw Error message
else
console.warn message if type is ABORT