Skip to content

Instantly share code, notes, and snippets.

@SBoudrias
Last active December 21, 2015 09:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SBoudrias/6286196 to your computer and use it in GitHub Desktop.
Save SBoudrias/6286196 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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