src/js/data.js
branchpopcorn-port
changeset 291 e942a49240f4
parent 248 fc01dc03f135
child 452 35495f156c41
equal deleted inserted replaced
290:1fd46a87bbc1 291:e942a49240f4
     1 /* data.js - this file deals with how the players gets and sends data */
     1 /* data.js - this file deals with how the players gets and sends data */
     2 
     2 
     3 IriSP.DataLoader = function() {
     3 IriSP.DataLoader = function() {
     4   this._cache = {};
     4   this._cache = {};
       
     5   
       
     6   /*
       
     7     A structure to hold callbacks for specific urls. We need it because
       
     8     ajax calls are asynchronous, so it means that sometimes we ask
       
     9     multiple times for a ressource because the first call hasn't been
       
    10     received yet.
       
    11   */
       
    12   this._callbacks = {};
     5 };
    13 };
     6 
    14 
     7 IriSP.DataLoader.prototype.get = function(url, callback) {
    15 IriSP.DataLoader.prototype.get = function(url, callback) {
     8 
    16 
     9   var base_url = url.split("&")[0]
    17   var base_url = url.split("&")[0]
    10   if (this._cache.hasOwnProperty(url)) {
    18   if (this._cache.hasOwnProperty(base_url)) {
    11     callback(this._cache[base_url]);
    19     callback(this._cache[base_url]);
    12   } else {
    20   } else {  
    13     /* we need a closure because this gets lost when it's called back */
    21     if (!this._callbacks.hasOwnProperty(base_url)) {
    14     // uncomment you don't want to use caching.
    22       this._callbacks[base_url] = [];
    15     // IriSP.jQuery.get(url, callback);
    23       this._callbacks[base_url].push(callback);   
       
    24       /* we need a closure because this gets lost when it's called back */
       
    25   
       
    26       // uncomment you don't want to use caching.
       
    27       // IriSP.jQuery.get(url, callback);
       
    28       
       
    29       var func = function(data) {
       
    30                   this._cache[base_url] = data;                                
       
    31                   var i = 0;
       
    32                   
       
    33                   for (i = 0; i < this._callbacks[base_url].length; i++) {
       
    34                     this._callbacks[base_url][i](this._cache[base_url]);                                  
       
    35                   }
       
    36       };
       
    37       
       
    38       IriSP.jQuery.get(url, IriSP.wrap(this, func));                                
    16     
    39     
    17     IriSP.jQuery.get(url, (function(obj) {      
    40     } else {
    18                                return function(data) {
    41       /* simply push the callback - it'll get called when the ressource
    19                                   obj._cache[base_url] = data;      
    42          has been received */
    20                                   callback(obj._cache[base_url]);
    43       
    21                                 }; 
    44       this._callbacks[base_url].push(callback);   
    22                               })(this));
    45    
    23     
    46     }
    24        
       
    25   }
    47   }
    26 }
    48 }
    27 
    49 
    28 /* the base abstract "class" */
    50 /* the base abstract "class" */
    29 IriSP.Serializer = function(DataLoader, url) {
    51 IriSP.Serializer = function(DataLoader, url) {