src/js/data.js
author hamidouk
Tue, 29 Nov 2011 11:09:08 +0100
branchpopcorn-port
changeset 345 8a088f7daa66
parent 291 e942a49240f4
child 452 35495f156c41
permissions -rw-r--r--
rollover over the interface buttons now works as expected. Also changed the width of the buttons to the correct size. Resized the width and height of the sprites to be the same as the boxes we display them in.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     1
/* data.js - this file deals with how the players gets and sends data */
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     2
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     3
IriSP.DataLoader = function() {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     4
  this._cache = {};
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
     5
  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
     6
  /*
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
     7
    A structure to hold callbacks for specific urls. We need it because
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
     8
    ajax calls are asynchronous, so it means that sometimes we ask
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
     9
    multiple times for a ressource because the first call hasn't been
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    10
    received yet.
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    11
  */
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    12
  this._callbacks = {};
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    13
};
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    14
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    15
IriSP.DataLoader.prototype.get = function(url, callback) {
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    16
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    17
  var base_url = url.split("&")[0]
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    18
  if (this._cache.hasOwnProperty(base_url)) {
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    19
    callback(this._cache[base_url]);
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    20
  } else {  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    21
    if (!this._callbacks.hasOwnProperty(base_url)) {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    22
      this._callbacks[base_url] = [];
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    23
      this._callbacks[base_url].push(callback);   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    24
      /* we need a closure because this gets lost when it's called back */
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    25
  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    26
      // uncomment you don't want to use caching.
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    27
      // IriSP.jQuery.get(url, callback);
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    28
      
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    29
      var func = function(data) {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    30
                  this._cache[base_url] = data;                                
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    31
                  var i = 0;
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    32
                  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    33
                  for (i = 0; i < this._callbacks[base_url].length; i++) {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    34
                    this._callbacks[base_url][i](this._cache[base_url]);                                  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    35
                  }
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    36
      };
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    37
      
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    38
      IriSP.jQuery.get(url, IriSP.wrap(this, func));                                
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    39
    
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    40
    } else {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    41
      /* simply push the callback - it'll get called when the ressource
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    42
         has been received */
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    43
      
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    44
      this._callbacks[base_url].push(callback);   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    45
   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    46
    }
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    47
  }
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    48
}
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    49
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    50
/* the base abstract "class" */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    51
IriSP.Serializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    52
  this._DataLoader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    53
  this._url = url;
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    54
  this._data = [];
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    55
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    56
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    57
IriSP.Serializer.prototype.serialize = function(data) { };
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    58
IriSP.Serializer.prototype.deserialize = function(data) {};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    59
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    60
IriSP.Serializer.prototype.currentMedia = function() {  
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    61
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    62
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    63
IriSP.Serializer.prototype.sync = function(callback) {  
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    64
  callback.call(this, this._data);  
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    65
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    66
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    67
IriSP.SerializerFactory = function(DataLoader) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    68
  this._dataloader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    69
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    70
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    71
IriSP.SerializerFactory.prototype.getSerializer = function(metadataOptions) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    72
  /* This function returns serializer set-up with the correct
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    73
     configuration - takes a metadata struct describing the metadata source
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    74
  */
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    75
  
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    76
  if (metadataOptions === undefined)
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    77
    /* return an empty serializer */
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    78
    return IriSP.Serializer("", "");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    79
            
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    80
  switch(metadataOptions.type) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    81
    case "json":
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    82
      return new IriSP.JSONSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    83
      break;
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    84
    
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    85
    case "dummy": /* only used for unit testing - not defined in production */
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    86
      return new IriSP.MockSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    87
      break;
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    88
    
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    89
    case "empty":
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    90
      return new IriSP.Serializer("", "empty");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    91
      break;
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    92
      
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    93
    default:      
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    94
      return undefined;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    95
  }
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    96
};