1 | REST = {
|
---|
2 |
|
---|
3 | resquests: {},
|
---|
4 |
|
---|
5 | taksResquests: {},
|
---|
6 |
|
---|
7 | isProcessRefreshToken: false,
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * This method is used to read resources from the server.
|
---|
11 | *
|
---|
12 | * @param uri Uri of the resource that gonna be readed.
|
---|
13 | * @param callback A function that is called when the resource is loaded successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
14 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
15 | * @return The target resource when the callback is ommitted ( made it synchronous )
|
---|
16 | */
|
---|
17 |
|
---|
18 | get: function( uri, data, callback, accept ){
|
---|
19 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'get', accept || 'json' ], data, callback, !!!callback);
|
---|
20 |
|
---|
21 | },
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This method is used to create resources from the server.
|
---|
25 | *
|
---|
26 | * @param uri Uri of the resource that gonna be readed.
|
---|
27 | * @param callback A function that is called when the resource is created on the server successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
28 | * @param accept The attribute accept that is used to ask the target format to the server.
|
---|
29 | * @return The result response of the create from the server when the callback is ommitted ( made it synchronous )
|
---|
30 | */
|
---|
31 |
|
---|
32 | post: function( uri, data, callback, accept ){
|
---|
33 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'post', accept || 'json' ], data, callback, !!!callback);
|
---|
34 | },
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * This method is used to update resources from the server.
|
---|
38 | *
|
---|
39 | * @param uri Uri of the resource that gonna be readed.
|
---|
40 | * @param callback A function that is called when the resource is update on the server successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
41 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
42 | * @return The result response of the update from the server when the callback is ommitted ( made it synchronous )
|
---|
43 | */
|
---|
44 |
|
---|
45 | put: function( uri, data, callback, accept ){
|
---|
46 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'put', accept || 'json' ], data, callback, !!!callback);
|
---|
47 |
|
---|
48 | },
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This method is used to delete resources from the server.
|
---|
52 | *
|
---|
53 | * @param uri Uri of the resource that gonna be readed.
|
---|
54 | * @param callback A function that is called when the resource is deleted successfully in the server. When the parameter is ignored the request is made synchrounsly.
|
---|
55 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
56 | * @return The result response of the delete from the server when the callback is ommitted ( made it synchronous )
|
---|
57 | */
|
---|
58 |
|
---|
59 | "delete": function( uri, callback, accept ){
|
---|
60 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'delete', accept || 'json' ], false, callback, !!!callback);
|
---|
61 | },
|
---|
62 |
|
---|
63 | send: function( url, type, data, callback, sync, extraOptions ){
|
---|
64 | this.id = url;
|
---|
65 |
|
---|
66 | if(REST.isProcessRefreshToken && !!callback){
|
---|
67 |
|
---|
68 | REST.taksResquests[REST.taksResquests.length] = {
|
---|
69 | url: url,
|
---|
70 | type: type,
|
---|
71 | data: data,
|
---|
72 | callback: callback,
|
---|
73 | sync: sync,
|
---|
74 | extraOptions: extraOptions
|
---|
75 | };
|
---|
76 |
|
---|
77 | }else if(REST.isProcessRefreshToken)
|
---|
78 | setTimeout(REST.send(url, type, data, callback, sync, extraOptions), 100);
|
---|
79 |
|
---|
80 | REST.resquests[url] = {
|
---|
81 | url: url,
|
---|
82 | type: type,
|
---|
83 | data: data,
|
---|
84 | callback: callback,
|
---|
85 | sync: sync,
|
---|
86 | extraOptions: extraOptions
|
---|
87 | };
|
---|
88 |
|
---|
89 | var result = false;
|
---|
90 | var envelope = {
|
---|
91 |
|
---|
92 | async: ( typeof sync !== "undefined" ? !sync : !!callback ),
|
---|
93 | url: url,
|
---|
94 | success: function( dt, textStatus, jqXHR ){
|
---|
95 |
|
---|
96 | delete REST.resquests[this.url];
|
---|
97 |
|
---|
98 | if(result == false){
|
---|
99 |
|
---|
100 | if( callback )
|
---|
101 | {
|
---|
102 | result = callback( dt, textStatus, jqXHR );
|
---|
103 | }
|
---|
104 | else
|
---|
105 | result = dt;
|
---|
106 | }
|
---|
107 |
|
---|
108 | },
|
---|
109 | error: function( dt, textStatus, jqXHR ){
|
---|
110 |
|
---|
111 | var response = {error: 'error', status: dt.status, description: dt.responseText, statusText: dt.responseText};
|
---|
112 |
|
---|
113 | if( callback )
|
---|
114 | {
|
---|
115 | result = callback( response, textStatus, jqXHR );
|
---|
116 | }
|
---|
117 | else
|
---|
118 | result = response;
|
---|
119 |
|
---|
120 | },
|
---|
121 |
|
---|
122 | type: $.isArray( type ) ? type[0] : type,
|
---|
123 | data: data
|
---|
124 |
|
---|
125 | };
|
---|
126 |
|
---|
127 | if( $.isArray( type ) && type[1] )
|
---|
128 | envelope['dataType'] = type[1];
|
---|
129 |
|
---|
130 | if( extraOptions )
|
---|
131 | envelope = $.extend( envelope, extraOptions );
|
---|
132 |
|
---|
133 | $.ajax( envelope );
|
---|
134 |
|
---|
135 | return( result );
|
---|
136 | },
|
---|
137 |
|
---|
138 | dispatch: function( dispatcher, data, callback, isPost, dataType ){
|
---|
139 | return this.send( this.dispatchPath + dispatcher + ".php",
|
---|
140 | [ ( isPost ? 'post' : 'get' ), dataType || 'json' ],
|
---|
141 | data, callback );
|
---|
142 | },
|
---|
143 |
|
---|
144 | dispatchPath: '../prototype',
|
---|
145 |
|
---|
146 | load: function(url, isRefresh){
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | } |
---|