forked from 0x2620/oxjs
updating Ox.App
This commit is contained in:
parent
d8d14cffe7
commit
6c0d7cd3ca
2 changed files with 69 additions and 115 deletions
|
|
@ -5,7 +5,7 @@ Application
|
|||
============================================================================
|
||||
*/
|
||||
|
||||
// fixme: get rid og launch, fire load event
|
||||
// fixme: get rid of launch, fire load event
|
||||
|
||||
Ox.App = (function() {
|
||||
|
||||
|
|
@ -22,42 +22,75 @@ Ox.App = (function() {
|
|||
Methods
|
||||
api[action] make a request
|
||||
api.cancel cancel a request
|
||||
launch launch the App
|
||||
options get or set options
|
||||
Events
|
||||
load app loaded
|
||||
***/
|
||||
|
||||
return function(options) {
|
||||
|
||||
options = options || {};
|
||||
var self = {},
|
||||
that = this;
|
||||
var self = {
|
||||
options: Ox.extend({
|
||||
timeout: 60000,
|
||||
type: 'POST',
|
||||
url: '/api/',
|
||||
}, options || {}),
|
||||
time: new Date()
|
||||
},
|
||||
that = Ox.Element();
|
||||
|
||||
self.time = +new Date();
|
||||
that.api = {
|
||||
api: function(callback) {
|
||||
Ox.Request.send({
|
||||
url: self.options.url,
|
||||
data: {
|
||||
action: 'api'
|
||||
},
|
||||
callback: callback
|
||||
});
|
||||
},
|
||||
cancel: function(id) {
|
||||
Ox.Request.cancel(id);
|
||||
}
|
||||
};
|
||||
|
||||
self.options = $.extend({
|
||||
timeout: 60000,
|
||||
type: 'POST',
|
||||
url: '/api/',
|
||||
}, options);
|
||||
$.ajaxSetup({
|
||||
timeout: self.options.timeout,
|
||||
type: self.options.type,
|
||||
url: self.options.url
|
||||
});
|
||||
|
||||
that.$element = new Ox.Element('body');
|
||||
|
||||
function getUserAgent() {
|
||||
var userAgent = '';
|
||||
Ox.forEach(['Chrome', 'Firefox', 'Internet Explorer', 'Opera', 'Safari'], function(v) {
|
||||
if (navigator.userAgent.indexOf(v) > -1) {
|
||||
userAgent = v;
|
||||
return false;
|
||||
}
|
||||
that.api.api(function(result) {
|
||||
Ox.forEach(result.data.actions, function(val, key) {
|
||||
that.api[key] = function(data, callback) {
|
||||
if (arguments.length == 1 && Ox.isFunction(data)) {
|
||||
callback = data;
|
||||
data = {};
|
||||
}
|
||||
return Ox.Request.send($.extend({
|
||||
url: self.options.url,
|
||||
data: {
|
||||
action: key,
|
||||
data: JSON.stringify(data)
|
||||
},
|
||||
callback: callback
|
||||
}, !val.cache ? {age: 0}: {}));
|
||||
};
|
||||
});
|
||||
if (!userAgent && $.browser.mozilla) {
|
||||
userAgent = 'Firefox';
|
||||
}
|
||||
if (!userAgent && $.browser.webkit) {
|
||||
userAgent = 'Chrome';
|
||||
}
|
||||
return userAgent;
|
||||
}
|
||||
that.api.init(getUserData(), function(result) {
|
||||
var $div = Ox.UI.$body.find('div');
|
||||
Ox.UI.$body.find('img').remove();
|
||||
$div.animate({
|
||||
opacity: 0
|
||||
}, 1000, function() {
|
||||
$div.remove();
|
||||
});
|
||||
that.triggerEvent({
|
||||
load: result.data
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function getUserData() {
|
||||
return {
|
||||
|
|
@ -91,81 +124,10 @@ Ox.App = (function() {
|
|||
|
||||
};
|
||||
|
||||
that.api = {
|
||||
api: function(callback) {
|
||||
Ox.Request.send({
|
||||
url: self.options.url,
|
||||
data: {
|
||||
action: 'api'
|
||||
},
|
||||
callback: callback
|
||||
});
|
||||
},
|
||||
cancel: function(id) {
|
||||
Ox.Request.cancel(id);
|
||||
}
|
||||
};
|
||||
|
||||
that.bindEvent = function() {
|
||||
|
||||
};
|
||||
|
||||
// fixme: use $.when()
|
||||
that.launch = function(callback) {
|
||||
var time = +new Date(),
|
||||
userAgent = getUserAgent(),
|
||||
userAgents = ['Chrome', 'Firefox', 'Opera', 'Safari'];
|
||||
$.ajaxSetup({
|
||||
timeout: self.options.timeour,
|
||||
type: self.options.type,
|
||||
url: self.options.url
|
||||
});
|
||||
userAgents.indexOf(userAgent) > -1 ? start() : stop();
|
||||
function start() {
|
||||
// fixme: rename config to site?
|
||||
var counter = 0, config, user;
|
||||
that.api.api(function(result) {
|
||||
Ox.forEach(result.data.actions, function(val, key) {
|
||||
that.api[key] = function(data, callback) {
|
||||
if (arguments.length == 1 && Ox.isFunction(data)) {
|
||||
callback = data;
|
||||
data = {};
|
||||
}
|
||||
return Ox.Request.send($.extend({
|
||||
url: self.options.url,
|
||||
data: {
|
||||
action: key,
|
||||
data: JSON.stringify(data)
|
||||
},
|
||||
callback: callback
|
||||
}, !val.cache ? {age: 0}: {}));
|
||||
};
|
||||
});
|
||||
that.api.init(getUserData(), function(result) {
|
||||
config = result.data.config;
|
||||
user = result.data.user;
|
||||
// fixme: not generic
|
||||
document.title = config.site.name;
|
||||
$(function() {
|
||||
var $div = Ox.UI.$body.find('div');
|
||||
Ox.UI.$body.find('img').remove();
|
||||
$div.animate({
|
||||
opacity: 0
|
||||
}, 1000, function() {
|
||||
$div.remove();
|
||||
});
|
||||
// fixme: not generic enough, just pass data
|
||||
callback({config: config, user: user});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
function stop() {
|
||||
that.request.send(self.options.init, getUserData(), function() {});
|
||||
}
|
||||
return that;
|
||||
};
|
||||
|
||||
that.options = function() {
|
||||
return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue