Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@SBoudrias /config.js
Last active

Using require.js map configuration to façade module in a way to always load relevant plugins and localization.
require.config({
paths: {
jquery : "../../vendors/js/jquery/jquery",
validate : "../../vendors/js/jquery.validation/jquery.validate",
validate_fr : "../../vendors/js/jquery.validation/messages_fr"
},
map: {
// Façade modules or swap them for another one
"*": {
"validate" : "facade-validator"
},
"facade/validator": {
"validate": "validate"
},
"validate_fr": {
"validate": "validate"
}
}
});
define([
"jquery",
"validate",
"validate_fr" // Get the good localization dictionnary - I'll usually use a require plugin here
], function( $ ) {
// Adding custom validation methods
$.validator.addMethod( "phone", function( value ) {
return value.match(/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i);
}, function() {
return "Please give a valid phone number";
});
return $.validator;
});
define([
"jquery",
"validate"
], function( $ ) {
$("form").validate();
});
@SBoudrias
Owner

For the i18n, I usually use require.replace loader plugin so I don't have to specify each locale in the map config, and to load/build only relevant locales without any headaches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.