src/js/data.js
author veltr
Wed, 11 Apr 2012 17:06:11 +0200
changeset 857 fa614dc66b0b
parent 842 4ae2247a59f4
child 859 002a16ff171b
permissions -rw-r--r--
Bugfix in Sparkline Widget
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
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    15
IriSP.DataLoader.prototype.get = function(url, callback, force_reload) {
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    16
  var base_url = url.split("&")[0];
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    17
  if (typeof force_reload != "undefined" && force_reload && this._cache.hasOwnProperty(base_url)) {
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    18
      delete this._cache[base_url]
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    19
  }
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    20
  if (this._cache.hasOwnProperty(base_url)) {
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    21
    callback(this._cache[base_url]);
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    22
  } else {  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    23
    if (!this._callbacks.hasOwnProperty(base_url)) {
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    24
      this._callbacks[base_url] = [callback];
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    25
      /* 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
    26
  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    27
      // uncomment you don't want to use caching.
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    28
      // IriSP.jQuery.get(url, callback);
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    29
      
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    30
      var func = function(data) {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    31
                  this._cache[base_url] = data;                                
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    32
                  var i = 0;
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    33
                  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    34
                  for (i = 0; i < this._callbacks[base_url].length; i++) {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    35
                    this._callbacks[base_url][i](this._cache[base_url]);                                  
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    36
                  }
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    37
                  delete this._callbacks[base_url];
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    38
      };
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    39
      
452
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    40
      /* automagically choose between json and jsonp */
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    41
      if (url.indexOf(document.location.hostname) === -1 &&
453
8568e47379a2 added autoconfiguration of the media source for rtmp streams.
hamidouk
parents: 452
diff changeset
    42
          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
    43
        // we contacting a foreign domain, use JSONP
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    44
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    45
        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
    46
      } else {
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    47
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    48
        // otherwise, hey, whatever rows your boat
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    49
        IriSP.jQuery.get(url, IriSP.wrap(this, func));
35495f156c41 automatically determine if we should use JSONP or something else.
hamidouk
parents: 291
diff changeset
    50
      }
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    51
    
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    52
    } else {
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    53
      /* simply push the callback - it'll get called when the ressource
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    54
         has been received */
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
      this._callbacks[base_url].push(callback);   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    57
   
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
    58
    }
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    59
  }
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    60
}
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    61
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    62
/* the base abstract "class" */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    63
IriSP.Serializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    64
  this._DataLoader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    65
  this._url = url;
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    66
  this._data = [];
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    67
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    68
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    69
IriSP.Serializer.prototype.serialize = function(data) { };
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    70
IriSP.Serializer.prototype.deserialize = function(data) {};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    71
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    72
IriSP.Serializer.prototype.currentMedia = function() {  
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    73
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    74
842
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    75
IriSP.Serializer.prototype.getDuration = function() {  
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    76
};
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    77
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    78
IriSP.Serializer.prototype.sync = function(callback) {
4ae2247a59f4 Changes for Cinecast
veltr
parents: 453
diff changeset
    79
  this._DataLoader.get(this._url, callback, force_refresh);
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    80
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    81
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    82
IriSP.SerializerFactory = function(DataLoader) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    83
  this._dataloader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    84
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    85
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    86
IriSP.SerializerFactory.prototype.getSerializer = function(metadataOptions) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    87
  /* This function returns serializer set-up with the correct
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    88
     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
    89
  */
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    90
  
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    91
  if (metadataOptions === undefined)
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    92
    /* return an empty serializer */
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    93
    return IriSP.Serializer("", "");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    94
            
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    95
  switch(metadataOptions.type) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    96
    case "json":
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    97
      return new IriSP.JSONSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    98
      break;
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    99
    
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
   100
    case "dummy": /* only used for unit testing - not defined in production */
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
   101
      return new IriSP.MockSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
   102
      break;
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   103
    
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   104
    case "empty":
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   105
      return new IriSP.Serializer("", "empty");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   106
      break;
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
   107
      
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
   108
    default:      
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
   109
      return undefined;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
   110
  }
291
e942a49240f4 caching for asynchronous request now works correctly.
hamidouk
parents: 248
diff changeset
   111
};