# samples and miscs

# utiliser des webservices retournant du xml avec AngularJS (ES5)

Utiliser jQuery.

Côté angular faire un call de webservice via $http.

Le xml retourné est dans res.data.

if (typeof(xml) === 'string') {
  xml = $.parseXML(xml);
}
1
2
3

Typer les données via des pseudos classes JavaScript et faire une methode parse dans chaque classe pour remplir un objet via un simple :

function MaClasse(data) {
  this.data1 = '';
  this.data2 = {};
  this.data3 = [];

  this.parse(data);
}

MaClasse.prototype.parse = function( data ) {
  // parsing xml attr
  this.data1 = data.attr('data1') || 'default_value';

  // parsing lone xml tag leading to a complex object
  if (data.find('>data2').length > 0) {
    this.data2 = new MaClasseData2( bloc.find('>data2') );
  }

  // parsing xml tag list leading to an array
  data.find('>data3').each(function () {
    that.data3.push(new MaClasseData3( $(this) ));
  });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Ce fonctionnement permet d'avoir des méthodes utilitaires dans le prototype de chaque type créé pour accéder au data via des règles métiers ou autre.

# télécharger un fichier (pdf, xls, ...) dans un nouvel onglet

Dans la promesse success du call au endpoint retournant le flux binaire :

  • créer un blob avec le type correspondant :
// où response.data correspond au flux de retour

// pdf
var file = new Blob([response.data], {type: 'application/pdf'});

// xls
var file = new Blob([response.data], {type: 'application/vnd.ms-excel'});
1
2
3
4
5
6
7
  • sauvegarder le fichier dans le temp du navigateur et ouvrir dans un nouvel onglet
if (window.navigator.msSaveOrOpenBlob) { // IE
    window.navigator.msSaveOrOpenBlob(file);
} else { // autres browsers
    var fileURL = window.URL.createObjectURL(file);
    window.open(fileURL, '_blank');
}
1
2
3
4
5
6