-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-advanced.js
executable file
·176 lines (159 loc) · 5.53 KB
/
backbone-advanced.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Advanced functions for backbone.js.
* Copyright (c) 2014 Evgeniy Blinov (https://evgeniyblinov.ru)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @link https://github.com/EvgeniyBlinov/JS for the canonical source repository
* @author Evgeniy Blinov <[email protected]>
* @copyright Copyright (c) 2014 Evgeniy Blinov
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
(function(){
'use strict';
/*****************MODEL ADVANCED FUNCTIONS*************************/
/**
* Init
* @return void
*/
Backbone.Model.prototype.initialize = function(options){
if (options) {
if (options._rules) {
this._rules = _.extend(this._rules, options._rules);
}
}
};
/**
* Validate
* @param {object} attributes
* @param {object} options
* @return mixed
*/
Backbone.Model.prototype.validate = function(attributes, options){
var self = this,
errors = {};
_.each( attributes, function( value, attribute ) {
var rule = self._rules[attribute];
if (rule) {
if (getType(rule) == 'Array') {
_.each(rule, function(currentRule){
if (!Backbone.utilities.Validator( value, currentRule )) {
var message = currentRule.message || 'Ошибка валидации данных!';
errors[attribute] = errors[attribute] || [];
errors[attribute].push(message);
}
});
} else {
if (!Backbone.utilities.Validator( value, rule )) {
var message = rule.message || 'Ошибка валидации данных!';
errors[attribute] = errors[attribute] || [];
errors[attribute].push(message);
}
}
}
});
if ( _.keys(errors).length > 0 ) {
return errors;
}
};
/*****************MODEL ADVANCED FUNCTIONS*************************/
/******************VIEW ADVANCED FUNCTIONS*************************/
/**
* Get form data
* @return object
*/
Backbone.View.prototype.getFormData = function(){
var self = this,
defaults = this.entity.defaults;
_.each(defaults, function(value, attribute){
defaults[attribute] = $(self.formSlctr + ' input[name="' + attribute + '"]').val().trim();
});
return defaults;
};
/**
* Get error block selector
* @return string
*/
Backbone.View.prototype.getErrorBlockSlctr = function(){
return (this.formErrorBlockOptions && this.formErrorBlockOptions.slctr) ? this.formErrorBlockOptions.slctr : 'error';
};
/**
* Create error block
* @return object of jQuery DOM element
*/
Backbone.View.prototype.createErrorBlock = function(){
if (this.formErrorBlockOptions) {
var EBOptions = this.formErrorBlockOptions;
var EBClass = (EBOptions.class) ? EBOptions.class : 'error';
var EBAttributes = (EBOptions.attributes) ? EBOptions.attributes.join(' ') : '';
return $('<div class="' + EBClass + '" ' + EBAttributes + '></div>');
} else {
return $('<div class="error"></div>');
}
};
/**
* Clear error block
* @return void
*/
Backbone.View.prototype.clearErrorBlock = function(){
$(this.formSlctr).find(this.getErrorBlockSlctr()).remove();
};
/**
* Create error message
* @param {string} message
* @return string
*/
Backbone.View.prototype.createErrorMessage = function( message ){
if (this.formErrorBlockOptions && this.formErrorBlockOptions.hasOwnProperty('template')) {
return this.formErrorBlockOptions.template({ message: message });
} else {
return '<p>' + message + '</p>';
}
};
/**
* Put error block
* @param {string} errorBlock
* @return void
*/
Backbone.View.prototype.putErrorBlock = function(errorBlock){
$(this.formSlctr).find('div.title').after(errorBlock);
};
/**
* Is error block active
* @return boolean
*/
Backbone.View.prototype.isErrorBlockActive = function(){
return !!$(this.formSlctr).find(this.getErrorBlockSlctr()).length;
};
/**
* Render errors
* @param {boolean} holdErrorBlock
* @return void
*/
Backbone.View.prototype.renderErrors = function(holdErrorBlock){
var self = this;
// if form has server errors
if (!holdErrorBlock) {
this.clearErrorBlock();
}
if (this.entity.validationError) {
var errorBlock = this.createErrorBlock();
_.each(this.entity.validationError, function( message, attribute ){
var errorMessage = self.createErrorMessage( message );
errorBlock.append(errorMessage);
});
this.putErrorBlock(errorBlock);
}
};
/**
* Render
* @param {boolean} holdErrorBlock
* @return void
*/
Backbone.View.prototype.render = function(holdErrorBlock){
holdErrorBlock = holdErrorBlock || false;
this.renderErrors(holdErrorBlock);
};
/******************VIEW ADVANCED FUNCTIONS*************************/
}).call(this);