src/js/model.js
branchnew-model
changeset 854 29f28a9f236f
child 856 41c574c807d1
equal deleted inserted replaced
853:3406e876a42e 854:29f28a9f236f
       
     1 /* model.js is where data is stored in a standard form, whatever the serializer */
       
     2 
       
     3 IriSP.Cinelab = {
       
     4     STATUS_WAITING : 1,
       
     5     STATUS_READY : 2
       
     6 }
       
     7 
       
     8 IriSP.Cinelab.Source = function(_directory, _url, _serializer) {
       
     9     this.status = IriSP.Cinelab.STATUS_EMPTY;
       
    10     if (typeof _directory === "undefined") {
       
    11         throw "Error : Cinelab.Source called with no parent directory";
       
    12     }
       
    13     if (typeof _url === "undefined") {
       
    14         throw "Error : Cinelab.Source called with no URL";
       
    15     }
       
    16     if (typeof _serializer === "undefined") {
       
    17         throw "Error : Cinelab.Source called with no serializer";
       
    18     }
       
    19     this.directory = _directory;
       
    20     this.serializer = _serializer;
       
    21     this.url = _url;
       
    22     this.callbackQueue = [];
       
    23     this.contents = null;
       
    24 }
       
    25 
       
    26 IriSP.Cinelab.Source.prototype.get = function() {
       
    27     IriSP.jQuery.getJSON(_url, function(_result) {
       
    28         this.contents = this.serializer.deSerialize(_result);
       
    29         if (this.callbackQueue.length) {
       
    30             var _this = this;
       
    31             IriSP._.each(this.callbackQueue, function(_callback) {
       
    32                 _callback.call(_this, this.contents);
       
    33             });
       
    34         }
       
    35         this.callbackQueue = [];
       
    36     });
       
    37 }
       
    38 
       
    39 IriSP.Cinelab.Source.prototype.addCallback = function(_callback) {
       
    40     if (this.status === IriSP.Cinelab.STATUS_READY) {
       
    41         callback.call(this, this.contents);
       
    42     } else {
       
    43         this.callbackQueue.push(_callback);
       
    44     }
       
    45 }
       
    46 
       
    47 IriSP.Cinelab.Directory = function() {
       
    48     this.sources = {};
       
    49     this.consolidated = [];
       
    50     this.imports = {};
       
    51 }
       
    52 
       
    53 IriSP.Cinelab.Directory.prototype.addSource = function(_source, _serializer) {
       
    54     this.source[_source] = new IriSP.Cinelab.Source(this, _source, _serializer);
       
    55 }
       
    56 
       
    57 IriSP.Cinelab.Directory.prototype.getSource = function(_source) {
       
    58     return (typeof this.sources[_source] !== "undefined" ? this.sources[_source] : false);
       
    59 }
       
    60 
       
    61 IriSP.Cinelab.Directory.prototype.source = function(_source, _serializer) {
       
    62     if (typeof this.sources[_source] !== "undefined") {
       
    63         this.addSource(_source, _serializer);
       
    64     }
       
    65     return this.getSource(_source);
       
    66 }