Eccoci nuovamente con le chiamate agli ADO.Net Data Service tramite jQuery, adesso vi mostro come scrivere una semplice ma comodissima estensione per jquery per chiamare gli ADO Service, sia in modo sincrono che in modo asincrono.

   1:  (function($) {
   2:      $.extend({
   3:          AdoDataService: function(urlService, async, dataService, successCallBack, errorCallBack) {
   4:              if (async) {
   5:                  $.getJSON(urlService, dataService, successCallBack);
   6:              } else {
   7:                  var serviceResult = $.ajax({
   8:                      url: urlService,
   9:                      data: dataService,
  10:                      type: 'GET',
  11:                      dataType: 'json',
  12:                      contentType: 'application/json',
  13:                      error: errorCallBack,
  14:                      success: successCallBack,
  15:                      async: false
  16:                  });
  17:                  return (serviceResult.status != 404 ? JSON.parse(serviceResult.responseText).d : null);
  18:              }
  19:          }
  20:      });
  21:  })(jQuery);

Con questa semplice funzione, chiamare gli ADO Service è un giochetto da ragazzi :

   1:  Example Asincrono :
   2:  $.AdoDataService("DataService.svc/Users", true, {}, 
   3:      function(result){
   4:           if (result.d != undefined){
   5:                $.each(function(i,v){
   6:                     alert($(v).attr('UserID'));
   7:               });
   8:          }
   9:      },
  10:      function(error){
  11:      }
  12:  );
  13:   
  14:  Example Sincrono :
  15:  var users = $.AdoDataService("DataService.svc/Users", false);

Cosa dire ragazzi fantastico.

Technorati Tag: ,,
Condividi Post :