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

@ -110,4 +110,4 @@ Scrollbars
.OxThemeClassic ::-webkit-scrollbar:active, .OxThemeClassic ::-webkit-scrollbar:active,
.OxThemeClassic ::-webkit-scrollbar-thumb:vertical:active { .OxThemeClassic ::-webkit-scrollbar-thumb:vertical:active {
background: rgb(192, 192, 192); background: rgb(192, 192, 192);
} }

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,50 +457,59 @@ requires
} }
function error(request, status, error) { function error(request, status, error) {
var $dialog = new Ox.Dialog({ try {
title: "Error: Remote request failed.", var result = JSON.parse(request.responseText);
buttons: [ } catch(e) {
new Ox.Button({ var result = {status: {code:request.status, text:request.statusText}};
value: "Details", }
click: function() { options.callback(result);
var $iframe = $("<iframe>"), if(result.status.code >= 500) {
iframe = $iframe[0].contentWindow; var $dialog = new Ox.Dialog({
iframe.document.open(); title: "Error: Remote request failed.",
iframe.document.write(xhr.responseText); buttons: [
iframe.document.close(); new Ox.Button({
$dialog.close(); value: "Details",
$dialog = new Ox.Dialog({ click: function() {
title: "Error: Remote request failed.", var $iframe = $("<iframe>");
buttons: [ var iframe = $iframe[0].contentWindow;
new Ox.Button({ iframe.document.open();
value: "Close", iframe.document.write(request.responseText);
click: function() { iframe.document.close();
$dialog.close(); $dialog.close();
} $dialog = new Ox.Dialog({
}) title: "Error: Remote request failed.",
], buttons: [
width: 800, new Ox.Button({
height: 400 value: "Close",
}) click: function() {
.append($iframe) $dialog.close();
.open(); }
} })
}), ],
new Ox.Button({ width: 800,
value: "Close", height: 400
click: function() { })
$dialog.close(); .append($iframe)
} .open();
}) }
] }),
}) new Ox.Button({
.append(request.status + " " + request.statusText) value: "Close",
.open(); click: function() {
Ox.print({ $dialog.close();
request: request, }
status: status, })
error: error ]
}); })
.append(request.status + " " + request.statusText)
.open();
Ox.print({
request: request,
status: status,
error: error
});
}
pending[options.id] = false; pending[options.id] = false;
} }

View file

@ -6,19 +6,26 @@
<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');
} }
}); });
}); });
</script> </script>
</head> </head>
<body></body> <body></body>
</html> </html>