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,19 +230,39 @@ 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) {
var user = data.data.user; $.each(result.data.actions, function(i, action) {
$(function() { that.api[action] = function(data, callback) {
var $div = $body.find('div'); if (arguments.length == 1) {
$body.find('img').remove(); callback = data;
$div.animate({ data = {};
opacity: 0 }
}, 1000, function() { console.log('api', action, data)
$div.remove(); console.log('sending', self.options.requestURL, {action: action, data: data})
}); return Ox.Request.send({
callback({ url: self.options.requestURL,
config: config, data: {
user: user action: action,
data: JSON.stringify(data)
},
callback: callback
});
};
});
that.api[self.options.init](getUserData(), function(data) {
var user = data.data.user;
$(function() {
var $div = $body.find('div');
$body.find('img').remove();
$div.animate({
opacity: 0
}, 1000, function() {
$div.remove();
});
callback({
config: config,
user: user
});
}); });
}); });
}); });
@ -236,7 +271,7 @@ 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,55 +6248,54 @@ 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) {
self.$pages[page] = new Ox.ListPage()
.css({
width: self.pageWidth + 'px'
});
if (self.options.orientation == 'horizontal') {
} else {
self.$pages[page].css({
top: (page * self.pageHeight + self.listMargin / 2) + 'px'
});
}
$.each(result.data.items, function(i, v) {
var pos = offset + i;
self.$items[pos] = new Ox.ListItem({
construct: self.options.construct,
data: v,
id: v[self.options.unique],
position: pos
});
self.ids[pos] = v[self.options.unique];
if (isSelected(pos)) {
self.$items[pos].addClass('OxSelected');
}
self.$items[pos].appendTo(self.$pages[page]);
});
if (self.options.type == 'text' && page == 0) {
var height = that.height() - (that.width() < that.$content.width() ? oxui.scrollbarSize : 0),
visibleItems = Math.ceil(height / self.options.itemHeight);
if (result.data.items.length < visibleItems) {
self.$pages[page].height(height).css({
overflow: 'hidden'
});
$.each(Ox.range(result.data.items.length, visibleItems), function(i, v) {
new Ox.ListItem({
construct: self.options.construct,
data: {},
id: '',
position: v
}).appendTo(self.$pages[page]);
});
}
}
self.$pages[page].appendTo(that.$content);
!Ox.isUndefined(callback) && callback();
},
keys: keys, keys: keys,
range: range, range: range,
sort: self.options.sort sort: self.options.sort
}, function(result) {
self.$pages[page] = new Ox.ListPage()
.css({
width: self.pageWidth + 'px'
});
if (self.options.orientation == 'horizontal') {
} else {
self.$pages[page].css({
top: (page * self.pageHeight + self.listMargin / 2) + 'px'
});
}
$.each(result.data.items, function(i, v) {
var pos = offset + i;
self.$items[pos] = new Ox.ListItem({
construct: self.options.construct,
data: v,
id: v[self.options.unique],
position: pos
});
self.ids[pos] = v[self.options.unique];
if (isSelected(pos)) {
self.$items[pos].addClass('OxSelected');
}
self.$items[pos].appendTo(self.$pages[page]);
});
if (self.options.type == 'text' && page == 0) {
var height = that.height() - (that.width() < that.$content.width() ? oxui.scrollbarSize : 0),
visibleItems = Math.ceil(height / self.options.itemHeight);
if (result.data.items.length < visibleItems) {
self.$pages[page].height(height).css({
overflow: 'hidden'
});
$.each(Ox.range(result.data.items.length, visibleItems), function(i, v) {
new Ox.ListItem({
construct: self.options.construct,
data: {},
id: '',
position: v
}).appendTo(self.$pages[page]);
});
}
}
self.$pages[page].appendTo(that.$content);
!Ox.isUndefined(callback) && callback();
})); }));
} else { } else {
self.$pages[page].appendTo(that.$content); self.$pages[page].appendTo(that.$content);
@ -6559,29 +6577,27 @@ 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(); self.pageLength = self.options.orientation == 'both' ?
self.pageLength = self.options.orientation == 'both' ? self.pageLengthByRowLength[self.rowLength] :
self.pageLengthByRowLength[self.rowLength] : self.options.pageLength;
self.options.pageLength; $.extend(self, {
$.extend(self, { listHeight: Math.ceil(result.data.items * (self.options.itemHeight + self.itemMargin) / self.rowLength), // fixme: should be listSize
listHeight: Math.ceil(result.data.items * (self.options.itemHeight + self.itemMargin) / self.rowLength), // fixme: should be listSize listLength: result.data.items,
listLength: result.data.items, pages: Math.ceil(result.data.items / self.pageLength),
pages: Math.ceil(result.data.items / self.pageLength), pageWidth: self.options.orientation == 'vertical' ? 0 :
pageWidth: self.options.orientation == 'vertical' ? 0 : (self.options.itemWidth + self.itemMargin) * self.rowLength,
(self.options.itemWidth + self.itemMargin) * self.rowLength, pageHeight: self.options.orientation == 'horizontal' ? 0 :
pageHeight: self.options.orientation == 'horizontal' ? 0 : Math.ceil(self.pageLength * (self.options.itemHeight + self.itemMargin) / self.rowLength)
Math.ceil(self.pageLength * (self.options.itemHeight + self.itemMargin) / self.rowLength) });
}); Ox.print('list self', self);
Ox.print('list self', self); that.$content.css({
that.$content.css({ height: self.listHeight + 'px'
height: self.listHeight + 'px' });
}); getPositions();
getPositions();
}
})); }));
} }