PDA

View Full Version : Moving moving custom validations to external files



ipavlic
26 Mar 2012, 3:31 AM
I'd like to move my custom validations that are repeated in my views to external files that I would load when needed. So for this validation:


Ext.apply(Ext.form.field.VTypes, {
oib: function(val, field) {
oib = val.toString();
if (oib.length != 11) return false;
var b = parseInt(oib, 10);
if (isNaN(b)) return false;
var a = 10;
for (var i = 0; i < 10; i++) {
a = a + parseInt(oib.substr(i, 1), 10);
a = a % 10;
if (a == 0) a = 10;
a *= 2;
a = a % 11;
}
var control = 11 - a;
if (control == 10) control = 0;
return control == parseInt(oib.substr(10, 1));
},
oibText: 'Oib broj nije ispravnog oblika. Provjerite duljinu i znamenke.',
oibMask: /[\d]/i
});

I'd like to be able to use something like

Ext.apply(Ext.form.field.VTypes, Ext.custom.validations.Oib);

How can I do that?

mitchellsimoens
26 Mar 2012, 8:17 AM
Issue is you will have dependancies. The file you are doing the Ext.apply on the VTypes depends on the other file with Ext.custom.validations.Oib stuff and you can run into a racing conditions.

ipavlic
26 Mar 2012, 8:28 AM
What's wrong with:

Put my files in

/ext4/custom
First define the path to my external files.

Ext.Loader.setConfig({
enabled : true,
paths : {
'custom' : '/ext4/custom'
}
});
Require the file

Ext.require([ 'Ext.*', 'custom.validations.Oib' ]);
Finally, apply the validation to singleton:

Ext.apply(Ext.form.field.VTypes, Ext.create('custom.validations.Oib'));

ipavlic
26 Mar 2012, 11:13 PM
Issue is you will have dependancies. The file you are doing the Ext.apply on the VTypes depends on the other file with Ext.custom.validations.Oib stuff and you can run into a racing conditions.

Let me check if I understand you right:
File A applies Oib Vtype using Oib file
File B applies Oib Vtype using Oib file

They could end up in racing conditions?

mitchellsimoens
27 Mar 2012, 4:13 AM
I thought you meant have the validations in one file and use Ext.apply referencing the validations in another file.

ipavlic
27 Mar 2012, 4:17 AM
I thought you meant have the validations in one file and use Ext.apply referencing the validations in another file.

Yes, I wanted to have that. The structure would be like this:
Oib.js
html page A with A.js using Oib.js to apply Vtype.
html page B with B.js using Oib.js to apply Vtype.