Test de création de pluggin
Bonjour,
pour test, j'ai créé un petit pluggin permettant juste de concaténer des chaines de caractères :
Code:
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 32 33 34 35 36 37
|
(function($){
$.customSerialize = function(params){
var obj = new CustomSerialize();
var i;
if(arguments.length > 0){
for(i=0; i<arguments.length; i++){
obj.add(arguments[i]);
}
}
return obj;
};
function CustomSerialize(){
this.str = "";
this.count = 0;
}
CustomSerialize.prototype = {
add: function(str){
if(this.count === 0){
this.str += $.trim(str);
} else {
this.str += "&" + $.trim(str);
}
this.count++;
return this;
},
getStr: function(){
return this.str;
}
};
})(jQuery); |
Il s'utilise comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10
|
var $customSerialize1 = $.customSerialize("a", "b", "c").add("d");
$customSerialize1.add("e");
var $customSerialize2 = $.customSerialize();
$customSerialize2.add("x");
$customSerialize2.add("y").add("z");
alert($customSerialize1.add("f").getStr());
alert($customSerialize2.getStr()); |
C'est mon premier test de pluggin : avant d'aller plus loin, j'aurai aimé savoir si je n'ai pas fait de bourdes (ça a l'air de fonctionner).
merci d'avance