adding app.api.foo(), and changing the signature of list.options.request to (data, callback)
This commit is contained in:
parent
314ddf7d75
commit
fe0acbb3ba
1 changed files with 120 additions and 104 deletions
|
@ -143,7 +143,7 @@ requires
|
|||
name: '',
|
||||
requestTimeout: 15000,
|
||||
requestType: 'POST',
|
||||
requestURL: '',
|
||||
requestURL: '', // fixme: should be apiURL etc
|
||||
}, options);
|
||||
|
||||
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) {
|
||||
var time = +new Date(),
|
||||
userAgent = getUserAgent(),
|
||||
|
@ -215,19 +230,39 @@ requires
|
|||
$.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() {
|
||||
$.getJSON(self.options.config, function(data) {
|
||||
var config = data;
|
||||
that.request(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
|
||||
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;
|
||||
$(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() {
|
||||
that.request(self.options.init, getUserData(), function() {});
|
||||
that.request.send(self.options.init, getUserData(), function() {});
|
||||
}
|
||||
return that;
|
||||
};
|
||||
|
@ -245,21 +280,6 @@ requires
|
|||
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;
|
||||
|
||||
};
|
||||
|
@ -288,7 +308,7 @@ requires
|
|||
function removeEvent(id, type, event, callback) {
|
||||
var focused = type == 'normal' || Ox.Focus.focused() == id,
|
||||
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])) {
|
||||
$.each(events[id][type], function(e, fns) {
|
||||
if (!event || event == e) {
|
||||
|
@ -642,7 +662,7 @@ requires
|
|||
Ox.Request = function() {
|
||||
|
||||
// fixme: do we want, instead of request('find', data, callback),
|
||||
// something like server.find(data, callback)?
|
||||
// something like api.find(data, callback)?
|
||||
|
||||
var cache = {},
|
||||
pending = {},
|
||||
|
@ -5912,7 +5932,7 @@ requires
|
|||
keys: [],
|
||||
orientation: 'vertical',
|
||||
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,
|
||||
sort: [],
|
||||
type: 'text',
|
||||
|
@ -6145,12 +6165,11 @@ requires
|
|||
// fixme: optimize: send non-selected ids if more than half of the items are selected
|
||||
if (self.selected.length /*&& self.selected.length < self.listLength*/) {
|
||||
self.requests.push(self.options.request({
|
||||
callback: getPositionsCallback,
|
||||
ids: $.map(self.selected, function(v, i) {
|
||||
return self.ids[v];
|
||||
}),
|
||||
sort: self.options.sort
|
||||
}));
|
||||
}, getPositionsCallback));
|
||||
} else {
|
||||
getPositionsCallback();
|
||||
}
|
||||
|
@ -6229,55 +6248,54 @@ requires
|
|||
self.pageLength : self.listLength % self.pageLength)];
|
||||
if (Ox.isUndefined(self.$pages[page])) {
|
||||
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,
|
||||
range: range,
|
||||
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 {
|
||||
self.$pages[page].appendTo(that.$content);
|
||||
|
@ -6559,29 +6577,27 @@ requires
|
|||
|
||||
function updateQuery() {
|
||||
clear();
|
||||
self.requests.push(self.options.request({
|
||||
callback: function(result) {
|
||||
var keys = {};
|
||||
that.triggerEvent('load', result.data);
|
||||
self.rowLength = getRowLength();
|
||||
self.pageLength = self.options.orientation == 'both' ?
|
||||
self.pageLengthByRowLength[self.rowLength] :
|
||||
self.options.pageLength;
|
||||
$.extend(self, {
|
||||
listHeight: Math.ceil(result.data.items * (self.options.itemHeight + self.itemMargin) / self.rowLength), // fixme: should be listSize
|
||||
listLength: result.data.items,
|
||||
pages: Math.ceil(result.data.items / self.pageLength),
|
||||
pageWidth: self.options.orientation == 'vertical' ? 0 :
|
||||
(self.options.itemWidth + self.itemMargin) * self.rowLength,
|
||||
pageHeight: self.options.orientation == 'horizontal' ? 0 :
|
||||
Math.ceil(self.pageLength * (self.options.itemHeight + self.itemMargin) / self.rowLength)
|
||||
});
|
||||
Ox.print('list self', self);
|
||||
that.$content.css({
|
||||
height: self.listHeight + 'px'
|
||||
});
|
||||
getPositions();
|
||||
}
|
||||
self.requests.push(self.options.request({}, function(result) {
|
||||
var keys = {};
|
||||
that.triggerEvent('load', result.data);
|
||||
self.rowLength = getRowLength();
|
||||
self.pageLength = self.options.orientation == 'both' ?
|
||||
self.pageLengthByRowLength[self.rowLength] :
|
||||
self.options.pageLength;
|
||||
$.extend(self, {
|
||||
listHeight: Math.ceil(result.data.items * (self.options.itemHeight + self.itemMargin) / self.rowLength), // fixme: should be listSize
|
||||
listLength: result.data.items,
|
||||
pages: Math.ceil(result.data.items / self.pageLength),
|
||||
pageWidth: self.options.orientation == 'vertical' ? 0 :
|
||||
(self.options.itemWidth + self.itemMargin) * self.rowLength,
|
||||
pageHeight: self.options.orientation == 'horizontal' ? 0 :
|
||||
Math.ceil(self.pageLength * (self.options.itemHeight + self.itemMargin) / self.rowLength)
|
||||
});
|
||||
Ox.print('list self', self);
|
||||
that.$content.css({
|
||||
height: self.listHeight + 'px'
|
||||
});
|
||||
getPositions();
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue