forked from cordjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRest.coffee
More file actions
123 lines (90 loc) · 3.26 KB
/
Rest.coffee
File metadata and controls
123 lines (90 loc) · 3.26 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
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
define [
'underscore'
( if window? then 'jquery' else '' )
], ( _, $ ) ->
class Rest
host: ''
port: 80
get: (data, callback) ->
options = _.extend {
type: 'GET'
}, data
@request options, callback
post: (data, callback) ->
options = _.extend {
type: 'POST'
}, data
@request options, callback
put: (data, callback) ->
options = _.extend {
type: 'PUT'
}, data
@request options, callback
del: (data, callback) ->
options = _.extend {
type: 'DELETE'
}, data
@request options, callback
browserUrlParse: (sUrl) ->
if $
urlParse = document.createElement 'a'
urlParse.href = sUrl
urlParse
browserRequest: (options, callback) ->
options.dataType = 'jsonp' if options.crossDomain?
$.ajax(options)
request: (options, callback) ->
options = _.extend {
type: 'GET'
}, options
# is browser
if $
restUrl = @browserUrlParse options.url
currUrl = @browserUrlParse '/'
if restUrl.hostname isnt currUrl.hostname
# todo: Нужно позже разобраться с кроссдоменным ajax через клиента, это возможно :)
# if options.method is 'GET'
# options.crossDomain = true
## options.url = "#{ options.url }"#?#{ $.param options.data }"
# else
# options.url = "/XDR/#{ encodeURIComponent options.url }"
# options.url = "#{ options.url }?#{ $.param options.data }"
options.url = "/XDR/#{ encodeURIComponent options.url }"
@browserRequest options, callback
else
setTimeout =>
require [ 'request', 'querystring', 'url' ], (request, qs, url) =>
parseUrl = url.parse options.url
options.method = options.type
options.url = "http://#{ @host }:#{ @port }#{ options.url }" if !parseUrl.host?
delete options.type
if options.dataType is 'json'
options.json = 'true'
delete options.dataType
if options.method == 'GET'
options.url += '?' + qs.stringify options.data if options.data?
request options, (error, response, body) ->
callback? body, error, response
if !error and response.statusCode is 200
ajaxCallbacks.success body
else
ajaxCallbacks.error arguments...
ajaxCallbacks.complete()
, 0
# сделано для того, чтобы сервер умел понимать Ajax .success, .error как jQuery
# это нужно для совместимости с моделями Spine без их потрашения
ajaxCallbacks =
success: ->
error: ->
complete: ->
returnCallbacks =
success: (callback) ->
ajaxCallbacks.success = callback
returnCallbacks
error: (callback) ->
ajaxCallbacks.error = callback
returnCallbacks
complete: (callback) ->
ajaxCallbacks.complete = callback
returnCallbacks
new Rest