Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
IMPLÉMENTATION DU TYPE GUID AVEC MICROSOFT AJAX
Information sur la source
Description
Un Guid est un numéro unique. En .net il existe le type System.Guid qui est décrit sur la msdn ici : http://msdn2.microsoft.com/en-us/library/system.guid.aspx J'ai réécrit ce type coté client en utiliser le framework Microsoft Ajax Library.
Source
- Type.registerNamespace('CS');
-
- CS.Guid = function(value){
- this._tabs = [];
-
- if (value.match('^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$')){
- this._tabs[0] = value.substring(0, 8).toUpperCase();
- this._tabs[1] = value.substring(9, 13).toUpperCase();
- this._tabs[2] = value.substring(14, 18).toUpperCase();
- this._tabs[3] = value.substring(19, 23).toUpperCase();
- this._tabs[4] = value.substring(24, 36).toUpperCase();
- } else if (value.match('^{[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}}$')){
- this._tabs[0] = value.substring(1, 9).toUpperCase();
- this._tabs[1] = value.substring(10, 14).toUpperCase();
- this._tabs[2] = value.substring(15, 19).toUpperCase();
- this._tabs[3] = value.substring(20, 24).toUpperCase();
- this._tabs[4] = value.substring(25, 37).toUpperCase();
- } else if (value.match('^\\([A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\\)$')){
- this._tabs[0] = value.substring(1, 9).toUpperCase();
- this._tabs[1] = value.substring(10, 14).toUpperCase();
- this._tabs[2] = value.substring(15, 19).toUpperCase();
- this._tabs[3] = value.substring(20, 24).toUpperCase();
- this._tabs[4] = value.substring(25, 37).toUpperCase();
- } else if (value.match('^[A-Za-z0-9]{32}$')){
- this._tabs[0] = value.substring(0, 8).toUpperCase();
- this._tabs[1] = value.substring(8, 12).toUpperCase();
- this._tabs[2] = value.substring(12, 16).toUpperCase();
- this._tabs[3] = value.substring(16, 20).toUpperCase();
- this._tabs[4] = value.substring(20, 32).toUpperCase();
- }else {
- throw Error.format(Sys.Res.formatInvalidString);
- }
- }
-
- CS.Guid.prototype = {
- toString : function(format){
- switch(format){
- case 'N' :
- return String.format('{0}{1}{2}{3}{4}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
- break;
-
- case 'B' :
- return String.format('{{{0}-{1}-{2}-{3}-{4}}}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
- break;
-
- case 'P' :
- return String.format('({0}-{1}-{2}-{3}-{4})', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
- break;
-
- case 'D' :
- case '' :
- case null :
- case undefined :
- return String.format('{0}-{1}-{2}-{3}-{4}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
- break;
-
- default :
- throw Error.format(Sys.Res.formatInvalidString);
- break;
- }
- }
- }
-
- CS.Guid.registerClass('CS.Guid');
-
- CS.Guid.empty = new CS.Guid('00000000-0000-0000-0000-000000000000');
- CS.Guid.newGuid = function(){
- var d = new Date();
- var end = d.getTime().toString();
- for(var i = end.length; i < 32; i++){
- end += Math.floor(Math.random()*16).toString(16);
- }
- return new CS.Guid(end);
- }
Type.registerNamespace('CS');
CS.Guid = function(value){
this._tabs = [];
if (value.match('^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$')){
this._tabs[0] = value.substring(0, 8).toUpperCase();
this._tabs[1] = value.substring(9, 13).toUpperCase();
this._tabs[2] = value.substring(14, 18).toUpperCase();
this._tabs[3] = value.substring(19, 23).toUpperCase();
this._tabs[4] = value.substring(24, 36).toUpperCase();
} else if (value.match('^{[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}}$')){
this._tabs[0] = value.substring(1, 9).toUpperCase();
this._tabs[1] = value.substring(10, 14).toUpperCase();
this._tabs[2] = value.substring(15, 19).toUpperCase();
this._tabs[3] = value.substring(20, 24).toUpperCase();
this._tabs[4] = value.substring(25, 37).toUpperCase();
} else if (value.match('^\\([A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\\)$')){
this._tabs[0] = value.substring(1, 9).toUpperCase();
this._tabs[1] = value.substring(10, 14).toUpperCase();
this._tabs[2] = value.substring(15, 19).toUpperCase();
this._tabs[3] = value.substring(20, 24).toUpperCase();
this._tabs[4] = value.substring(25, 37).toUpperCase();
} else if (value.match('^[A-Za-z0-9]{32}$')){
this._tabs[0] = value.substring(0, 8).toUpperCase();
this._tabs[1] = value.substring(8, 12).toUpperCase();
this._tabs[2] = value.substring(12, 16).toUpperCase();
this._tabs[3] = value.substring(16, 20).toUpperCase();
this._tabs[4] = value.substring(20, 32).toUpperCase();
}else {
throw Error.format(Sys.Res.formatInvalidString);
}
}
CS.Guid.prototype = {
toString : function(format){
switch(format){
case 'N' :
return String.format('{0}{1}{2}{3}{4}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
break;
case 'B' :
return String.format('{{{0}-{1}-{2}-{3}-{4}}}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
break;
case 'P' :
return String.format('({0}-{1}-{2}-{3}-{4})', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
break;
case 'D' :
case '' :
case null :
case undefined :
return String.format('{0}-{1}-{2}-{3}-{4}', this._tabs[0], this._tabs[1], this._tabs[2], this._tabs[3], this._tabs[4]);
break;
default :
throw Error.format(Sys.Res.formatInvalidString);
break;
}
}
}
CS.Guid.registerClass('CS.Guid');
CS.Guid.empty = new CS.Guid('00000000-0000-0000-0000-000000000000');
CS.Guid.newGuid = function(){
var d = new Date();
var end = d.getTime().toString();
for(var i = end.length; i < 32; i++){
end += Math.floor(Math.random()*16).toString(16);
}
return new CS.Guid(end);
}
Conclusion
Contrairement à un vrai guid, le numéro n'est pas forcément unique (un guid est généré aléatoirement en prenant en compte la date, l'adresse mac de la carte réseau et d'autres paramètre) et on peut facilement connaitre l'heure de la création d'un guid. Mais dans la plupart des cas ce type nous suffit largement côté client.
Historique
- 18 avril 2007 20:29:56 :
- correction d'une boulette
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 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 | | | |
|
|