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);
};
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;
};
@ -442,50 +457,59 @@ requires
}
function error(request, status, error) {
var $dialog = new Ox.Dialog({
title: "Error: Remote request failed.",
buttons: [
new Ox.Button({
value: "Details",
click: function() {
var $iframe = $("<iframe>"),
iframe = $iframe[0].contentWindow;
iframe.document.open();
iframe.document.write(xhr.responseText);
iframe.document.close();
$dialog.close();
$dialog = new Ox.Dialog({
title: "Error: Remote request failed.",
buttons: [
new Ox.Button({
value: "Close",
click: function() {
$dialog.close();
}
})
],
width: 800,
height: 400
})
.append($iframe)
.open();
}
}),
new Ox.Button({
value: "Close",
click: function() {
$dialog.close();
}
})
]
})
.append(request.status + " " + request.statusText)
.open();
Ox.print({
request: request,
status: status,
error: 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({
title: "Error: Remote request failed.",
buttons: [
new Ox.Button({
value: "Details",
click: function() {
var $iframe = $("<iframe>");
var iframe = $iframe[0].contentWindow;
iframe.document.open();
iframe.document.write(request.responseText);
iframe.document.close();
$dialog.close();
$dialog = new Ox.Dialog({
title: "Error: Remote request failed.",
buttons: [
new Ox.Button({
value: "Close",
click: function() {
$dialog.close();
}
})
],
width: 800,
height: 400
})
.append($iframe)
.open();
}
}),
new Ox.Button({
value: "Close",
click: function() {
$dialog.close();
}
})
]
})
.append(request.status + " " + request.statusText)
.open();
Ox.print({
request: request,
status: status,
error: error
});
}
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">
$(function() {
var app = new Ox.App().launch();
Ox.Request.send({
url: "http://blackbook.local:8000/api/",
data: {
"function": "hello",
data: JSON.stringify({key: "value"})
},
callback: function(data) {
console.log(data);
var app = new Ox.App({
requestURL:"http://blackbook.local:8000/api/"
});
app.request('hello');
app.request('login', {'username':'test', 'password':'test'}, function(result) {
Ox.print(result);
if(result.status.code == 200) {
Ox.print(result);
app.request('preferences', function(result) {
Ox.print(result.data.preferences);
});
}
else if(result.status.code == 403) {
alert('login failed');
} else {
Ox.print('broken');
}
});
});