зеркало из https://github.com/mozilla/popcorn-js.git
[#220] Popcorn.xhr support needed for parsing data-timeline-sources
This commit is contained in:
Родитель
12bad186b3
Коммит
4599b6eb3d
56
popcorn.js
56
popcorn.js
|
@ -542,6 +542,62 @@
|
|||
return plugin;
|
||||
};
|
||||
|
||||
|
||||
var setup = {
|
||||
url: '',
|
||||
data: '',
|
||||
dataType: '',
|
||||
success: Popcorn.nop,
|
||||
type: 'GET',
|
||||
async: true,
|
||||
xhr: function() {
|
||||
return new XMLHttpRequest();
|
||||
}
|
||||
};
|
||||
|
||||
Popcorn.xhr = function ( options ) {
|
||||
|
||||
var settings = Popcorn.extend( {}, setup, options );
|
||||
|
||||
settings.ajax = settings.xhr();
|
||||
|
||||
if ( settings.ajax ) {
|
||||
|
||||
settings.ajax.open( settings.type, settings.url, settings.async );
|
||||
settings.ajax.send( null );
|
||||
|
||||
return Popcorn.xhr.httpData( settings );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Popcorn.xhr.httpData = function ( settings ) {
|
||||
|
||||
var data, json = null,
|
||||
|
||||
onreadystatechange = settings.ajax.onreadystatechange = function() {
|
||||
|
||||
if ( settings.ajax.readyState == 4 ) {
|
||||
|
||||
try {
|
||||
json = JSON.parse(settings.ajax.responseText);
|
||||
} catch(e) {
|
||||
//suppress
|
||||
};
|
||||
|
||||
data = {
|
||||
xml: settings.ajax.responseXML,
|
||||
text: settings.ajax.responseText,
|
||||
json: json
|
||||
};
|
||||
|
||||
settings.success.call( settings.ajax, data );
|
||||
|
||||
}
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
global.Popcorn = Popcorn;
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{ "data": {"lang": "en", "length": 25} }
|
|
@ -0,0 +1 @@
|
|||
{ "data": {"lang": "en", "length": 25} }
|
|
@ -0,0 +1 @@
|
|||
This is a text test
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<dashboard>
|
||||
<locations class="foo">
|
||||
<location for="bar">
|
||||
<infowindowtab>
|
||||
<tab title="Location"><![CDATA[blabla]]></tab>
|
||||
<tab title="Users"><![CDATA[blublu]]></tab>
|
||||
</infowindowtab>
|
||||
</location>
|
||||
</locations>
|
||||
</dashboard>
|
|
@ -662,8 +662,120 @@ test("Protected Names", function () {
|
|||
};
|
||||
});
|
||||
});
|
||||
Popcorn.xhr.httpData
|
||||
|
||||
module("Popcorn XHR");
|
||||
test("Basic", function () {
|
||||
|
||||
expect(2);
|
||||
|
||||
equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function");
|
||||
equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function");
|
||||
|
||||
|
||||
});
|
||||
|
||||
test("Parsing text", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop();
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.txt',
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
equals( data.text, "This is a text test", "test.txt returns the string 'This is a text test'");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("Parsing JSON", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop();
|
||||
|
||||
|
||||
var testObj = { "data": {"lang": "en", "length": 25} };
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.js',
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
|
||||
ok( QUnit.equiv(data.json, testObj) , "data.json returns an object of data");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test("Parsing XML", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop();
|
||||
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.xml',
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
var parser = new DOMParser(),
|
||||
xml = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><dashboard><locations class="foo"><location for="bar"><infowindowtab> <tab title="Location"><![CDATA[blabla]]></tab> <tab title="Users"><![CDATA[blublu]]></tab> </infowindowtab> </location> </locations> </dashboard>',"text/xml");
|
||||
|
||||
|
||||
equals( data.xml.toString(), xml.toString(), "data.xml returns a document of xml");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
module("Popcorn Test Runner End");
|
||||
test("Last Check", function () {
|
||||
|
||||
// ALWAYS RUN LAST
|
||||
|
|
Загрузка…
Ссылка в новой задаче