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