-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotRouter.js
More file actions
103 lines (90 loc) · 3.04 KB
/
BotRouter.js
File metadata and controls
103 lines (90 loc) · 3.04 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
/**
* Роутер для рендеринга страниц для ботов.
*/
Ext.define('B.BotRouter', {
/**
* Рендеринг необходимой страницы.
* @param {Object} request Объект запроса Express.
* @param {Object} response Объект ответа Express.
*/
render: function (request, response) {
var originalPath = request.path;
var path = originalPath;
if (!path || path === '/') {
path = '/page-root-search';
}
path = this.normalizePath(path);
path = this.removeRegisterId(path);
path = this.removeCompanyId(path);
this.getContextData(originalPath, function (error, data) {
if (error) {
response.status(500).send(error);
} else {
response.render(path, data);
}
}, this);
},
privates: {
/**
* @private
* @param {String} path Путь.
* @return {String} Путь.
*/
normalizePath: function (path) {
return path
.replace(/\//g, '')
.replace(/_/g, '-')
.replace(/-/g, '/');
},
/**
* @private
* @param {String} path Путь.
* @return {String} Путь.
*/
removeRegisterId: function (path) {
if (/register\//.test(path)) {
path.split('/').slice(0, -1).join('/');
}
return path;
},
/**
* @private
* @param {String} path Путь.
* @return {String} Путь.
*/
removeCompanyId: function (path) {
if (/company\//.test(path)) {
path = path.split('/');
path.splice(2, 1);
path = path.join('/');
}
return path;
},
/**
* @private
* @param {String} path Путь.
* @param {Function} callback Следующий шаг, получает аргумент ошибки и аргумент данных.
* @param {Object} scope Контекст исполнения следующего шага.
*/
getContextData: function (path, callback, scope) {
if (!/company/.test(path)) {
callback.call(scope, null, {});
return;
}
var id = path.replace(/_/g, '-').split('-')[2];
Ext.create('B.mongo.Company', {
id: id,
scope: this,
success: function (data) {
callback.call(scope, null, data);
},
failure: function () {
callback.call(scope, 'Ошибка получения данных', {});
},
badIdCallback: function () {
callback.call(scope, 'Не валидный идентификатор компании', {});
}
}).getCompany();
}
}
});