Skip to content

Commit

Permalink
Add ready callbacks on Stream and Project
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Reed committed Mar 2, 2012
1 parent 9e4ecfd commit 2275a33
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions viewer/js/coreref.base.js
Expand Up @@ -8,6 +8,25 @@ coreref.Stream = function(options) {
// private variables
var _this = this;
var _data = [];
var _ready = false;
var _readyQueue = [];

// handle json data
var handleData = function(json) {
for (var i = 0; i < json.data.length; i++) {
_this.add(json.data[i]);
}
if (json.paging && json.paging.next) {
jQuery.getJSON(root + json.paging.next, function(foo) {
handleData(foo);
});
} else {
_ready = true;
for (var i = 0; i < _readyQueue.length; i++) {
_readyQueue[i](_this);
}
}
}

// public fields
this.name = options.name;
Expand All @@ -24,23 +43,43 @@ coreref.Stream = function(options) {
}
_data.splice(insert, 0, item);
}
this.ready = function(func) {
if (_ready) {
func.call(this);
} else {
_readyQueue.push(func);
}
}

// initialization code
if (options.data) {
for (var i = 0; i < options.data.length; i++) {
this.add(options.data[i]);
}
_ready = true;
} else if (options.url) {
var root = options.root || '';
jQuery.getJSON(root + options.url, function(data) {
console.log(data);
jQuery.getJSON(root + options.url, function(json) {
handleData(json);
});
}
}

coreref.Project = function(options) {
// private vairables
var _this;
var _init = false;
var _readyCount = 0;
var _readyQueue = [];

var handleStreamReady = function(stream) {
_readyCount--;
if (_readyCount <= 0) {
for (var i = 0; i < _readyQueue.length; i++) {
_readyQueue[i](_this);
}
}
}

// public fields
this.id;
Expand All @@ -51,6 +90,9 @@ coreref.Project = function(options) {
this.root;

this.init = function(options) {
if (!options) { return; }
_init = true;
_this = this;
this.id = options.id || "";
this.name = options.name || "";
this.description = options.description || "";
Expand All @@ -64,12 +106,27 @@ coreref.Project = function(options) {
var s = options.streams[i];
if (s instanceof coreref.Stream) {
this.streams.push(s);
_readyCount++;
s.ready(handleStreamReady);
} else {
this.streams.push(new coreref.Stream(s));
if (this.root) {
s.root = this.root;
}
var stream = new coreref.Stream(s);
this.streams.push(stream);
_readyCount++;
stream.ready(handleStreamReady);
}
}
}
}
this.ready = function(func) {
if (_init && _readyCount <= 0) {
func(this);
} else {
_readyQueue.push(func);
}
}

// initialize the project
this.init(options);
Expand Down

0 comments on commit 2275a33

Please sign in to comment.