adding app.api.foo(), and changing the signature of list.options.request to (data, callback)

This commit is contained in:
rolux 2010-09-13 15:54:22 +02:00
parent 314ddf7d75
commit fe0acbb3ba

View file

@ -143,7 +143,7 @@ requires
name: '', name: '',
requestTimeout: 15000, requestTimeout: 15000,
requestType: 'POST', requestType: 'POST',
requestURL: '', requestURL: '', // fixme: should be apiURL etc
}, options); }, options);
function getUserAgent() { function getUserAgent() {
@ -198,6 +198,21 @@ requires
}; };
that.api = {
api: function(callback) {
Ox.Request.send({
url: self.options.requestURL,
data: {
action: 'api'
},
callback: callback
});
},
cancel: function(id) {
Ox.Request.cancel(id);
}
};
that.launch = function(callback) { that.launch = function(callback) {
var time = +new Date(), var time = +new Date(),
userAgent = getUserAgent(), userAgent = getUserAgent(),
@ -215,7 +230,26 @@ requires
$.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() { $.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() {
$.getJSON(self.options.config, function(data) { $.getJSON(self.options.config, function(data) {
var config = data; var config = data;
that.request(self.options.init, getUserData(), function(data) { that.api.api(function(result) {
$.each(result.data.actions, function(i, action) {
that.api[action] = function(data, callback) {
if (arguments.length == 1) {
callback = data;
data = {};
}
console.log('api', action, data)
console.log('sending', self.options.requestURL, {action: action, data: data})
return Ox.Request.send({
url: self.options.requestURL,
data: {
action: action,
data: JSON.stringify(data)
},
callback: callback
});
};
});
that.api[self.options.init](getUserData(), function(data) {
var user = data.data.user; var user = data.data.user;
$(function() { $(function() {
var $div = $body.find('div'); var $div = $body.find('div');
@ -234,9 +268,10 @@ requires
}); });
}); });
}); });
});
} }
function stop() { function stop() {
that.request(self.options.init, getUserData(), function() {}); that.request.send(self.options.init, getUserData(), function() {});
} }
return that; return that;
}; };
@ -245,21 +280,6 @@ requires
return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that); return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that);
}; };
that.request = function(action, data, callback) {
if (arguments.length == 2) {
callback = data;
data = {};
}
return Ox.Request.send({
url: self.options.requestURL,
data: {
action: action,
data: JSON.stringify(data)
},
callback: callback
});
};
return that; return that;
}; };
@ -288,7 +308,7 @@ requires
function removeEvent(id, type, event, callback) { function removeEvent(id, type, event, callback) {
var focused = type == 'normal' || Ox.Focus.focused() == id, var focused = type == 'normal' || Ox.Focus.focused() == id,
toString = (callback || '').toString(); toString = (callback || '').toString();
Ox.print('removeEvent', id, type, event, callback); Ox.print('removeEvent', id, type, event/*, callback*/);
if (events[id] && events[id][type] && (!event || events[id][type][event])) { if (events[id] && events[id][type] && (!event || events[id][type][event])) {
$.each(events[id][type], function(e, fns) { $.each(events[id][type], function(e, fns) {
if (!event || event == e) { if (!event || event == e) {
@ -642,7 +662,7 @@ requires
Ox.Request = function() { Ox.Request = function() {
// fixme: do we want, instead of request('find', data, callback), // fixme: do we want, instead of request('find', data, callback),
// something like server.find(data, callback)? // something like api.find(data, callback)?
var cache = {}, var cache = {},
pending = {}, pending = {},
@ -5912,7 +5932,7 @@ requires
keys: [], keys: [],
orientation: 'vertical', orientation: 'vertical',
pageLength: 100, pageLength: 100,
request: function() {}, // (sort:, range:, callback:), without parameter returns {items, size etc.} request: function() {}, // (data, callback), without data returns {items, size etc.}
//rowLength: 1, //rowLength: 1,
sort: [], sort: [],
type: 'text', type: 'text',
@ -6145,12 +6165,11 @@ requires
// fixme: optimize: send non-selected ids if more than half of the items are selected // fixme: optimize: send non-selected ids if more than half of the items are selected
if (self.selected.length /*&& self.selected.length < self.listLength*/) { if (self.selected.length /*&& self.selected.length < self.listLength*/) {
self.requests.push(self.options.request({ self.requests.push(self.options.request({
callback: getPositionsCallback,
ids: $.map(self.selected, function(v, i) { ids: $.map(self.selected, function(v, i) {
return self.ids[v]; return self.ids[v];
}), }),
sort: self.options.sort sort: self.options.sort
})); }, getPositionsCallback));
} else { } else {
getPositionsCallback(); getPositionsCallback();
} }
@ -6229,7 +6248,10 @@ requires
self.pageLength : self.listLength % self.pageLength)]; self.pageLength : self.listLength % self.pageLength)];
if (Ox.isUndefined(self.$pages[page])) { if (Ox.isUndefined(self.$pages[page])) {
self.requests.push(self.options.request({ self.requests.push(self.options.request({
callback: function(result) { keys: keys,
range: range,
sort: self.options.sort
}, function(result) {
self.$pages[page] = new Ox.ListPage() self.$pages[page] = new Ox.ListPage()
.css({ .css({
width: self.pageWidth + 'px' width: self.pageWidth + 'px'
@ -6274,10 +6296,6 @@ requires
} }
self.$pages[page].appendTo(that.$content); self.$pages[page].appendTo(that.$content);
!Ox.isUndefined(callback) && callback(); !Ox.isUndefined(callback) && callback();
},
keys: keys,
range: range,
sort: self.options.sort
})); }));
} else { } else {
self.$pages[page].appendTo(that.$content); self.$pages[page].appendTo(that.$content);
@ -6559,8 +6577,7 @@ requires
function updateQuery() { function updateQuery() {
clear(); clear();
self.requests.push(self.options.request({ self.requests.push(self.options.request({}, function(result) {
callback: function(result) {
var keys = {}; var keys = {};
that.triggerEvent('load', result.data); that.triggerEvent('load', result.data);
self.rowLength = getRowLength(); self.rowLength = getRowLength();
@ -6581,7 +6598,6 @@ requires
height: self.listHeight + 'px' height: self.listHeight + 'px'
}); });
getPositions(); getPositions();
}
})); }));
} }