-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractRouter.js
More file actions
114 lines (101 loc) · 3.69 KB
/
AbstractRouter.js
File metadata and controls
114 lines (101 loc) · 3.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
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
/**
* Абстрактный роутер обработки входящих запросов.
* Способен обрабатывать как конкретные запросы,
* так и переадресовывать запросы другим роутерам.
*
*
* Для задания карты роутинга необходимо заполнить
* объект конфигурации map в таком виде:
*
* map: {
* 'urlPath': {
* 'methodNameLowerCase': 'handlerAsString or linkToFunction'
* }
* }
*
*
* Для задания карты переадресации запроса другим роутерам необходимо заполнить
* объект конфигурации delegate в таком виде:
*
* delegate: {
* 'urlPath': 'routerClassNameAsString'
* }
*
*
* *Порядок ссылок имеет значение.*
* Если в начале указать слишком общую ссылку - следующие ниже ссылки
* не будут обработаны т.к. соответствие паттерну запроса было найдено раньше.
* Ссылки делегирования обрабатываются в конце.
*/
Ext.define('B.AbstractRouter', {
config: {
/**
* @cfg {Object} Карта роутинга, смотри описание класса.
*/
map: null,
/**
* @cfg {Object} Схема делегирования,смотри описание класса.
*/
delegate: null,
/**
* @cfg {Object} Объект Express роутера.
*/
expressRouter: null
},
constructor: function (config) {
this.initConfig(
Ext.apply(
Ext.clone(this.config),
config
)
);
this.makeExpressRouter();
this.initMap();
this.initDelegate();
},
/**
* Создает и сохраняет Express роутер.
*/
makeExpressRouter: function () {
var express = B.Main.getExpress();
this.setExpressRouter(express.Router());
},
/**
* Инициализация карты роутинга.
*/
initMap: function () {
Ext.Object.each(this.getMap(), function (path, config) {
Ext.Object.each(config, function (method, handler) {
if (Ext.isString(handler)) {
handler = this[handler];
}
this.getExpressRouter()[method](path, handler.bind(this));
}, this);
}, this);
},
/**
* Инициализация схемы делегирования.
*/
initDelegate: function () {
Ext.Object.each(this.getDelegate(), function (path, routerName) {
var routerImpl = Ext.create(routerName);
var target = routerImpl.getExpressRouter();
this.getExpressRouter().use(path, target);
}, this);
},
/**
* Валидирует модель.
* В случае если модель не валидна - отправляет клиенту сообщение об ошибке.
* @param {Ext.data.Model} model Модель запроса.
* @param {Object} response Express объект ответа сервера.
* @return {Boolean} Результат валидации.
*/
checkRequestModel: function (model, response) {
if (model.isValid()) {
return true;
} else {
B.Protocol.sendInvalidParams(response);
return false
}
}
});