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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
dojo.declare('SimpleSaveWriteStore', dojo.data.ItemFileWriteStore, {
_postUrl: 'postdata.php',
_saveCustom: function(saveCompleteCallback, saveFailedCallback){
dojo.xhrPost({
url: this._postUrl,
content: {
data: this.meToJson()
},
load: saveCompleteCallback,
error: saveFailedCallback
});
},
itemToJson : function(item) {
var json = {};
if (item) {
var attributes = this.getAttributes(item);
if (attributes && attributes.length > 0) {
var i;
for (i = 0; i < attributes.length; i++) {
var values = this.getValues(item, attributes[i]);
if (values) {
if (values.length > 1 ) {
var j;
json[attributes[i]] = [];
for (j = 0; j < values.length; j++ ) {
var value = values[j];
if (this.isItem(value)) {json[attributes[i]].push(dojo.fromJson(this.itemToJson(value)));}
else {json[attributes[i]].push(value);}
}
}
else{
if (store.isItem(values[0])){json[attributes[i]] = dojo.fromJson(this.itemToJson(values[0]));}
else {json[attributes[i]] = values[0];}
}
}
}
}
}
return dojo.toJson(json);
},
meToJson: function() {
var me = this;
var data = "{'identifier':'id','label':'libelle','items':[";
this.fetch({
onComplete:function(items){
items.forEach(function(item){data += me.itemToJson(item); data+= ",";})
}
});
data += "]}"
return data;
}
}); |
Partager