src/js/serializers/JSONSerializer.js
author hamidouk
Thu, 20 Oct 2011 15:59:28 +0200
branchpopcorn-port
changeset 108 62da43e72e30
child 128 f3fec80dd31c
permissions -rw-r--r--
broke the serializers across multiple files. added a newline to the end of segmentsWidget.js.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     1
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     2
IriSP.JSONSerializer = function(DataLoader, url) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     3
  IriSP.Serializer.call(this, DataLoader, url);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     4
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     5
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     6
IriSP.JSONSerializer.prototype = IriSP.Serializer;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     7
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     8
IriSP.JSONSerializer.prototype.serialize = function(data) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     9
  return JSON.stringify(data);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    10
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    11
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    12
IriSP.JSONSerializer.prototype.deserialize = function(data) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    13
  return JSON.parse(data);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    14
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    15
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    16
IriSP.JSONSerializer.prototype.sync = function(callback) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    17
  /* we don't have to do much because jQuery handles json for us */
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    18
  
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    19
  /* a wrapper to get a closure because we lose this in callbacks */
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    20
  var wrapper = function(obj) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    21
    return function(data) {    
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    22
      obj._data = data;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    23
      // sort the data too
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    24
      obj._data["annotations"].sort(function(a, b) 
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    25
          { var a_begin = +a.begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    26
            var b_begin = +b.begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    27
            return a_begin - b_begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    28
          });
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    29
      callback(data);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    30
    }
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    31
  };
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    32
  
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    33
  this._DataLoader.get(this._url, wrapper(this));
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    34
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    35
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    36
IriSP.JSONSerializer.prototype.currentMedia = function() {  
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    37
  return this._data.medias[0]; /* FIXME: don't hardcode it */
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    38
};