1 /* data.js - this file deals with how the players gets and sends data */ |
|
2 |
|
3 IriSP.DataLoader = function() { |
|
4 this._cache = {}; |
|
5 |
|
6 /* |
|
7 A structure to hold callbacks for specific urls. We need it because |
|
8 ajax calls are asynchronous, so it means that sometimes we ask |
|
9 multiple times for a ressource because the first call hasn't been |
|
10 received yet. |
|
11 */ |
|
12 this._callbacks = {}; |
|
13 }; |
|
14 |
|
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 } |
|
20 if (this._cache.hasOwnProperty(base_url)) { |
|
21 callback(this._cache[base_url]); |
|
22 } else { |
|
23 if (!this._callbacks.hasOwnProperty(base_url)) { |
|
24 this._callbacks[base_url] = [callback]; |
|
25 /* we need a closure because this gets lost when it's called back */ |
|
26 |
|
27 // uncomment you don't want to use caching. |
|
28 // IriSP.jQuery.get(url, callback); |
|
29 |
|
30 var func = function(data) { |
|
31 this._cache[base_url] = data; |
|
32 var i = 0; |
|
33 |
|
34 for (i = 0; i < this._callbacks[base_url].length; i++) { |
|
35 this._callbacks[base_url][i](this._cache[base_url]); |
|
36 } |
|
37 delete this._callbacks[base_url]; |
|
38 }; |
|
39 |
|
40 /* automagically choose between json and jsonp */ |
|
41 if (url.indexOf(document.location.hostname) === -1 && |
|
42 url.indexOf("http://") !== -1 /* not a relative url */ ) { |
|
43 // we contacting a foreign domain, use JSONP |
|
44 |
|
45 IriSP.jQuery.get(url, {}, IriSP.wrap(this, func), "jsonp"); |
|
46 } else { |
|
47 |
|
48 // otherwise, hey, whatever rows your boat |
|
49 IriSP.jQuery.get(url, IriSP.wrap(this, func)); |
|
50 } |
|
51 |
|
52 } else { |
|
53 /* simply push the callback - it'll get called when the ressource |
|
54 has been received */ |
|
55 |
|
56 this._callbacks[base_url].push(callback); |
|
57 |
|
58 } |
|
59 } |
|
60 } |
|
61 |
|
62 /* the base abstract "class" */ |
|
63 IriSP.Serializer = function(DataLoader, url) { |
|
64 this._DataLoader = DataLoader; |
|
65 this._url = url; |
|
66 this._data = []; |
|
67 }; |
|
68 |
|
69 IriSP.Serializer.prototype.serialize = function(data) { }; |
|
70 IriSP.Serializer.prototype.deserialize = function(data) {}; |
|
71 |
|
72 IriSP.Serializer.prototype.currentMedia = function() { |
|
73 }; |
|
74 |
|
75 IriSP.Serializer.prototype.getDuration = function() { |
|
76 }; |
|
77 |
|
78 IriSP.Serializer.prototype.sync = function(callback) { |
|
79 this._DataLoader.get(this._url, callback, force_refresh); |
|
80 }; |
|
81 |
|
82 IriSP.SerializerFactory = function(DataLoader) { |
|
83 this._dataloader = DataLoader; |
|
84 }; |
|
85 |
|
86 IriSP.SerializerFactory.prototype.getSerializer = function(metadataOptions) { |
|
87 /* This function returns serializer set-up with the correct |
|
88 configuration - takes a metadata struct describing the metadata source |
|
89 */ |
|
90 |
|
91 if (metadataOptions === undefined) |
|
92 /* return an empty serializer */ |
|
93 return IriSP.Serializer("", ""); |
|
94 |
|
95 switch(metadataOptions.type) { |
|
96 case "json": |
|
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; |
|
103 |
|
104 case "empty": |
|
105 return new IriSP.Serializer("", "empty"); |
|
106 break; |
|
107 |
|
108 default: |
|
109 return undefined; |
|
110 } |
|
111 }; |
|