-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.js
121 lines (98 loc) · 3.81 KB
/
Base.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
define([],
function(){
// module:
// mystique/Base
var Base = function(){},
mixin = {
// summary:
// The base Validator module that all other Validator modules
// should inherit from.
//haltOnPass: boolean
// If this validator is part of a ValidatorGroup, setting
// to true will stop any Validators after this one
// from executing if this validator passes
//haltOnPass: false,
//haltOnFail: boolean
// If this validator is part of a ValidatorGroup, setting
// to true will stop any Validators after this one
// from executing if this validator fails
//haltOnFail: false,
//skipOnPass:
// If this validator is part of a ValidatorGroup, setting
// to true will skip evaluating this validator, if all previous
// validators have already evaluated to true
//skipOnPass: false,
//skipOnFail:
// If this validator is part of a ValidatorGroup, setting
// to true will skip evaluating this validator, if a previous
// validator has already evaluated to false
//skipOnFail: false,
useCache: true,
//_validatedValuesCache: undefined,
isValidator: true,
maxCacheSize: 50,
isValid: function(value){
var valueString = JSON.stringify(value);
if (this.useCache){
if (! this._validatedValuesCache){
this._validatedValuesCache = {};
}
var cacheItem = this._validatedValuesCache[valueString];
if(cacheItem){
return cacheItem;
}
}
var result = this._isValid(value);
if (this.useCache){
this._addToCache(valueString, result);
}
return result;
},
_isValid: function(value){
// summary:
// Should be overridden by inheriting modules.
// Will check if the supplied value is valid or not.
// If the value is invalid, should populate the messages array.
//
// returns:
// instance of mystique/Result
},
_addToCache: function(valueString, result){
var self = this,
cacheResult = function(result){
var value = result.get('value');
if (value.then){
result.value.then(function(result){
cacheResult(result);
});
} else {
if (self._validatedValuesCache.length > self.maxCacheSize){
self._validatedValuesCache.shift();
}
self._validatedValuesCache[valueString] = result;
}
};
cacheResult(result);
}
},
i;
for (i in mixin){
Base.prototype[i] = mixin[i];
}
Base.extend = function(mixin){
var Child = function(options){
for (i in options){
this[i] = options[i];
}
};
Child.prototype = new this;
Child.prototype.constructor = Child;
Child.constructor = this.prototype.constructor;
for (i in mixin){
Child.prototype[i] = mixin[i];
}
Child.extend = this.extend;
return Child;
}
return Base;
});