New Model first commit new-model
authorveltr
Fri, 06 Apr 2012 19:13:08 +0200
branchnew-model
changeset 854 29f28a9f236f
parent 853 3406e876a42e
child 856 41c574c807d1
New Model first commit
src/js/model.js
src/js/serializers/CinecastSerializer.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/js/model.js	Fri Apr 06 19:13:08 2012 +0200
@@ -0,0 +1,66 @@
+/* model.js is where data is stored in a standard form, whatever the serializer */
+
+IriSP.Cinelab = {
+    STATUS_WAITING : 1,
+    STATUS_READY : 2
+}
+
+IriSP.Cinelab.Source = function(_directory, _url, _serializer) {
+    this.status = IriSP.Cinelab.STATUS_EMPTY;
+    if (typeof _directory === "undefined") {
+        throw "Error : Cinelab.Source called with no parent directory";
+    }
+    if (typeof _url === "undefined") {
+        throw "Error : Cinelab.Source called with no URL";
+    }
+    if (typeof _serializer === "undefined") {
+        throw "Error : Cinelab.Source called with no serializer";
+    }
+    this.directory = _directory;
+    this.serializer = _serializer;
+    this.url = _url;
+    this.callbackQueue = [];
+    this.contents = null;
+}
+
+IriSP.Cinelab.Source.prototype.get = function() {
+    IriSP.jQuery.getJSON(_url, function(_result) {
+        this.contents = this.serializer.deSerialize(_result);
+        if (this.callbackQueue.length) {
+            var _this = this;
+            IriSP._.each(this.callbackQueue, function(_callback) {
+                _callback.call(_this, this.contents);
+            });
+        }
+        this.callbackQueue = [];
+    });
+}
+
+IriSP.Cinelab.Source.prototype.addCallback = function(_callback) {
+    if (this.status === IriSP.Cinelab.STATUS_READY) {
+        callback.call(this, this.contents);
+    } else {
+        this.callbackQueue.push(_callback);
+    }
+}
+
+IriSP.Cinelab.Directory = function() {
+    this.sources = {};
+    this.consolidated = [];
+    this.imports = {};
+}
+
+IriSP.Cinelab.Directory.prototype.addSource = function(_source, _serializer) {
+    this.source[_source] = new IriSP.Cinelab.Source(this, _source, _serializer);
+}
+
+IriSP.Cinelab.Directory.prototype.getSource = function(_source) {
+    return (typeof this.sources[_source] !== "undefined" ? this.sources[_source] : false);
+}
+
+IriSP.Cinelab.Directory.prototype.source = function(_source, _serializer) {
+    if (typeof this.sources[_source] !== "undefined") {
+        this.addSource(_source, _serializer);
+    }
+    return this.getSource(_source);
+}