Last active
December 21, 2015 09:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define([ | |
| "jquery", | |
| "validate" | |
| ], function( $ ) { | |
| $("form").validate(); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the i18n, I usually use
require.replaceloader plugin so I don't have to specify each locale in themapconfig, and to load/build only relevant locales without any headaches.