1
|
|
|
/** |
2
|
|
|
* Item name |
3
|
|
|
* |
4
|
|
|
* Info |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
function Server() { |
13
|
|
|
|
14
|
|
|
} |
15
|
|
|
Server.prototype.runCommands = function (commands) { |
16
|
|
|
for (var key in commands) { |
|
|
|
|
17
|
|
|
var command = commands[key]; |
18
|
|
|
var callPath = command.call.split('.'); |
19
|
|
|
var curPath = window; |
20
|
|
|
for (var keyPath in callPath) { |
|
|
|
|
21
|
|
|
if (typeof curPath[callPath[keyPath]] == 'undefined') { |
22
|
|
|
console.log('undefined call path ' + callPath[keyPath] + ' in ' + command.call); |
|
|
|
|
23
|
|
|
curPath = null; |
24
|
|
|
break; |
25
|
|
|
} |
26
|
|
|
curPath = curPath[callPath[keyPath]]; |
27
|
|
|
} |
28
|
|
|
if (curPath !== null) { |
29
|
|
|
curPath.apply(null, command.params); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
Server.prototype.request = function (options, btn) { |
34
|
|
|
var ajaxOptions = { |
35
|
|
|
url: '', |
36
|
|
|
type: 'GET', |
37
|
|
|
dataType: 'json', |
38
|
|
|
data: {}, |
39
|
|
|
async: true, |
40
|
|
|
contentType: false, |
41
|
|
|
cache: false, |
42
|
|
|
}; |
43
|
|
|
for (var key in options) { |
|
|
|
|
44
|
|
|
ajaxOptions[key] = options[key]; |
45
|
|
|
} |
46
|
|
|
if (options.url && options.url.indexOf(inji.options.appRoot) !== 0) { |
47
|
|
|
ajaxOptions.url = inji.options.appRoot + (options.url.replace(/^\//g, '')); |
48
|
|
|
} |
49
|
|
|
if (typeof btn != 'undefined') { |
50
|
|
|
$(btn).data('loading-text', 'подождите'); |
51
|
|
|
var btn = $(btn).button().button('loading'); |
52
|
|
|
} |
53
|
|
|
var callback = null; |
54
|
|
|
if (typeof options.success != 'undefined') { |
55
|
|
|
callback = options.success; |
56
|
|
|
} |
57
|
|
|
ajaxOptions.success = function (data, textStatus, jqXHR) { |
58
|
|
|
if (typeof btn != 'undefined') { |
59
|
|
|
btn.button('reset'); |
60
|
|
|
} |
61
|
|
|
if (ajaxOptions.dataType != 'json') { |
62
|
|
|
callback(data, textStatus, jqXHR); |
63
|
|
|
} else { |
64
|
|
|
if (data.success) { |
65
|
|
|
if (data.successMsg) { |
66
|
|
|
noty({text: data.successMsg, type: 'success', timeout: 3500, layout: 'center'}); |
67
|
|
|
} |
68
|
|
|
if (typeof data.scripts == 'object') { |
69
|
|
|
inji.loaded = false; |
70
|
|
|
inji.onLoad(function () { |
71
|
|
|
inji.Server.runCommands(data.commands); |
72
|
|
|
if (callback !== null) { |
73
|
|
|
callback(data.content, textStatus, jqXHR) |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
if (data.scripts.length > 0) { |
77
|
|
|
inji.loadScripts(data.scripts, 0); |
78
|
|
|
} else { |
79
|
|
|
inji.startCallbacks(); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
inji.Server.runCommands(data.commands); |
83
|
|
|
if (callback !== null) { |
84
|
|
|
callback(data.content, textStatus, jqXHR); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
inji.Server.runCommands(data.commands); |
89
|
|
|
noty({text: data.error, type: 'warning', timeout: 3500, layout: 'center'}); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
var errorCallback = null; |
94
|
|
|
if (typeof options.error != 'undefined') { |
95
|
|
|
errorCallback = options.error; |
96
|
|
|
} |
97
|
|
|
ajaxOptions.error = function (jqXHR, textStatus, errorThrown) { |
98
|
|
|
if (typeof btn != 'undefined') { |
99
|
|
|
btn.button('reset'); |
100
|
|
|
} |
101
|
|
|
if (errorCallback != null) { |
102
|
|
|
errorCallback(jqXHR, textStatus, errorThrown); |
103
|
|
|
} else if (textStatus != 'abort') { |
104
|
|
|
noty({text: 'Во время запроса произошла ошибка: ' + textStatus, type: 'warning', timeout: 3500, layout: 'center'}); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return $.ajax(ajaxOptions); |
108
|
|
|
}; |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: