add app.request, extend demo

This commit is contained in:
j 2010-01-27 18:55:37 +05:30
parent d57a14d048
commit d9848bacf7
3 changed files with 86 additions and 55 deletions

View file

@ -105,6 +105,21 @@ requires
return Ox.getset(self.options, Array.slice.call(arguments), self.change, that); return Ox.getset(self.options, Array.slice.call(arguments), self.change, that);
}; };
that.request = function(fn, data, callback) {
if(Ox.isFunction(data)) {
callback = data;
data = {};
}
return Ox.Request.send({
url: self.options.requestURL,
data: {
"function": fn,
data: JSON.stringify(data)
},
callback: callback
});
};
return that; return that;
}; };
@ -442,16 +457,23 @@ requires
} }
function error(request, status, error) { function error(request, status, error) {
try {
var result = JSON.parse(request.responseText);
} catch(e) {
var result = {status: {code:request.status, text:request.statusText}};
}
options.callback(result);
if(result.status.code >= 500) {
var $dialog = new Ox.Dialog({ var $dialog = new Ox.Dialog({
title: "Error: Remote request failed.", title: "Error: Remote request failed.",
buttons: [ buttons: [
new Ox.Button({ new Ox.Button({
value: "Details", value: "Details",
click: function() { click: function() {
var $iframe = $("<iframe>"), var $iframe = $("<iframe>");
iframe = $iframe[0].contentWindow; var iframe = $iframe[0].contentWindow;
iframe.document.open(); iframe.document.open();
iframe.document.write(xhr.responseText); iframe.document.write(request.responseText);
iframe.document.close(); iframe.document.close();
$dialog.close(); $dialog.close();
$dialog = new Ox.Dialog({ $dialog = new Ox.Dialog({
@ -486,6 +508,8 @@ requires
status: status, status: status,
error: error error: error
}); });
}
pending[options.id] = false; pending[options.id] = false;
} }

View file

@ -6,15 +6,22 @@
<script type="text/javascript" src="../../build/js/ox.ui.js"></script> <script type="text/javascript" src="../../build/js/ox.ui.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
var app = new Ox.App().launch(); var app = new Ox.App({
Ox.Request.send({ requestURL:"http://blackbook.local:8000/api/"
url: "http://blackbook.local:8000/api/", });
data: { app.request('hello');
"function": "hello", app.request('login', {'username':'test', 'password':'test'}, function(result) {
data: JSON.stringify({key: "value"}) Ox.print(result);
}, if(result.status.code == 200) {
callback: function(data) { Ox.print(result);
console.log(data); app.request('preferences', function(result) {
Ox.print(result.data.preferences);
});
}
else if(result.status.code == 403) {
alert('login failed');
} else {
Ox.print('broken');
} }
}); });
}); });