src/js/data.js
author hamidouk
Tue, 24 Jan 2012 17:30:01 +0100
branchembed-playerapi-rewrite
changeset 702 f1225d38c150
parent 453 8568e47379a2
child 842 4ae2247a59f4
permissions -rw-r--r--
new, extendable api.
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
      
452
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    38
      /* automagically choose between json and jsonp */
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    39
      if (url.indexOf(document.location.hostname) === -1 &&
453
8568e47379a2 added autoconfiguration of the media source for rtmp streams.
hamidouk
parents: 452
diff changeset
    40
          url.indexOf("http://") !== -1 /* not a relative url */ ) {
452
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    41
        // we contacting a foreign domain, use JSONP
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    42
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    43
        IriSP.jQuery.get(url, {}, IriSP.wrap(this, func), "jsonp");
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    44
      } else {
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    45
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    46
        // otherwise, hey, whatever rows your boat
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    47
        IriSP.jQuery.get(url, IriSP.wrap(this, func));
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    48
      }
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    49
    
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    50
    } else {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    51
      /* simply push the callback - it'll get called when the ressource
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    52
         has been received */
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    53
      
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    54
      this._callbacks[base_url].push(callback);   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    55
   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    56
    }
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    57
  }
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    58
}
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    59
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    60
/* the base abstract "class" */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    61
IriSP.Serializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    62
  this._DataLoader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    63
  this._url = url;
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    64
  this._data = [];
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    65
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    66
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    67
IriSP.Serializer.prototype.serialize = function(data) { };
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    68
IriSP.Serializer.prototype.deserialize = function(data) {};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    69
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    70
IriSP.Serializer.prototype.currentMedia = function() {  
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    71
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    72
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    73
IriSP.Serializer.prototype.sync = function(callback) {  
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    74
  callback.call(this, this._data);  
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    75
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    76
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    77
IriSP.SerializerFactory = function(DataLoader) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    78
  this._dataloader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    79
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    80
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    81
IriSP.SerializerFactory.prototype.getSerializer = function(metadataOptions) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    82
  /* This function returns serializer set-up with the correct
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    83
     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
    84
  */
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    85
  
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    86
  if (metadataOptions === undefined)
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    87
    /* return an empty serializer */
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    88
    return IriSP.Serializer("", "");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    89
            
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    90
  switch(metadataOptions.type) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    91
    case "json":
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    92
      return new IriSP.JSONSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    93
      break;
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    94
    
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    95
    case "dummy": /* only used for unit testing - not defined in production */
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    96
      return new IriSP.MockSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    97
      break;
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    98
    
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    99
    case "empty":
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   100
      return new IriSP.Serializer("", "empty");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   101
      break;
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
   102
      
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   103
    default:      
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
   104
      return undefined;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
   105
  }
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
   106
};