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