HTTPRequest.prototype.resolve = function() { var cache = null; if (Ti.Network.online) { cache = this.getCachedResponse(); if (cache != null) { this.defer.resolve(cache); } else { this.send(); } } else { Event.trigger('http.offline'); if (exports.config.offlineCache === true || this.opt.offlineCache === true) { cache = this.getCachedResponse(); if (cache != null) { this.defer.resolve(); } else { this.defer.reject({ offline: true, message: L('network_offline', 'Check your connectivity.') }); } } else { this.defer.reject({ offline: true, message: L('network_offline', 'Check your connectivity.') }); } } };
.then(function() { var payload = { id: currentUser.id }; opt.success(payload); if (opt.silent !== true) { Event.trigger('auth.success', payload); } })
exports.logout = function(callback) { Event.trigger('auth.logout', { id: exports.getUserID() }); var driver = getStoredDriverString(); if (driver != null) { exports.loadDriver(driver).logout(); } var logoutUrl = (driver && driver.config ? driver.config.logoutUrl : null) || exports.config.logoutUrl; HTTP.send({ url: logoutUrl, method: 'POST', timeout: 3000, complete: function() { currentUser = null; Prop.removeProperty('auth.me'); Cache.purge(); if (exports.config.useOAuth == true) { exports.OAuth.resetCredentials(); } else { Ti.Network.removeHTTPCookiesForDomain(Util.getDomainFromURL(HTTP.config.base)); } if (_.isFunction(callback)) callback(); } }); };
success: function(payload) { if (timeouted) return; opt.success(payload); if (opt.silent != true) { Event.trigger('auth.success', payload); } },
Ti.App.addEventListener('resumed', function() { launchURL = Util.parseSchema(); if (launchURL !== pauseURL) { Event.trigger('app.resumed', { url: launchURL }); } });
success: function() { currentUser = Alloy.createModel('user', Prop.getObject('auth.me')); var payload = { id: currentUser.id, offline: true }; opt.success(payload); if (opt.silent !== true) { Event.trigger('auth.success', payload); } },
.then(function() { if (timeouted) return; var payload = { id: currentUser.id, oauth: true }; opt.success(payload); if (opt.silent !== true) { Event.trigger('auth.success', payload); } })
function onWindowClose(e) { var route = e.source._route; Event.trigger('flow.close', { route: route, }); var index = -1; for (var k in windowsStack) { if (windowsStack[k]._route === route) { windowsStack.splice(+k, 1); return; } } }
HTTPRequest.prototype._send = function() { var self = this; var client = Ti.Network.createHTTPClient({ timeout: this.timeout, cache: false, }); client.onload = client.onerror = function(e) { self._whenComplete(e); }; // Add this request to the queue exports.addToQueue(this); if (this.opt.silent !== true) { Event.trigger('http.start', { hash: this.hash, eventName: this.opt.eventName }); } // Progress callbacks if (_.isFunction(this.opt.ondatastream)) client.ondatastream = this.opt.ondatastream; if (_.isFunction(this.opt.ondatasend)) client.ondatasend = this.opt.ondatasend; client.open(this.method, this.url); // Set file receiver if (this.opt.file != null) { client.file = this.opt.file; } // Set headers _.each(this.headers, function(h, k) { client.setRequestHeader(k, h); }); // Send the request over Internet this.startTime = Date.now(); if (this.data != null) { client.send(this.data); } else { client.send(); } this.client = client; };
function onNotificationReceived(e) { if (OS_ANDROID) { // Android trigger two types of callback // When the app is in background, the type is !== 'callback' // So, we simply save the state inBackground and return // because the notification.received event must NOT be triggered if (e.type !== 'callback') { inBackground = true; return; } if (e.payload != null) { // Do this to balance the difference in APIs (convert Android to IOS, in substance) e.data = Util.parseJSON(e.payload); if (e.data.android != null) { _.extend(e.data, e.data.android); delete e.data.android; } // Set the property inBackground from the last state // and reset to false to prevent double events (simple semaphore) e.inBackground = inBackground; inBackground = false; } } // Auto-reset the badge if (exports.config.autoReset) { exports.resetBadge(); } // Router if (exports.config.useRouter) { if (e.inBackground) { if (e.data.url != null) { Router.go(e.data.url); } } } Event.trigger('notification.received', e); }
HTTPRequest.prototype._onComplete = function(e) { this.endTime = new Date(); if (this.onComplete !== null) this.onComplete(); HTTP.removeFromQueue(this); // Fire the global event if (this.opt.silent !== true) { Event.trigger('http.end', { hash: this.hash, eventName: this.opt.eventName }); } this.responseInfo = this._getResponseInfo(); // If the readyState is not DONE, trigger error, because // client.onload is the function to be called upon a SUCCESSFULL response. if (this.responseInfo.broken === true) { this._onError({}); return; } var data = this.client.responseData; var text = extractHTTPText(this.client.responseText, this.responseInfo); if (e.success === true) { // Write the cache (if needed and supported by configuration) this._maybeCacheResponse(); Ti.API.debug('HTTP: ['+this.hash+'] SUCCESS in '+(this.endTime-this.startTime)+'ms'); if (this.onSuccess !== null) this.onSuccess(text, data); } else { this._onError({ message: Util.getErrorMessage(text), code: this.client.status, response: text }); } };
HTTPRequest.prototype._whenComplete = function(e) { this.endTime = Date.now(); exports.removeFromQueue(this); // Fire the global event if (this.opt.silent !== true) { Event.trigger('http.end', { hash: this.hash, eventName: this.opt.eventName }); } try { this.responseInfo = this._getResponseInfo(); } catch (ex) { this.defer.reject({ broken: true }); return; } var data = null; if (this.opt.format === 'blob') { data = this.client.responseData; } else { data = extractHTTPText(this.client.responseText, this.responseInfo); } if (e.success) { this._maybeCacheResponse(data); this.defer.resolve(data); } else { this.defer.reject({ message: (this.opt.format === 'blob') ? null : Util.getErrorMessage(data), error: e.error, code: this.client.status, response: data }); } };
HTTPRequest.prototype.send = function() { this.client = Ti.Network.createHTTPClient({ timeout: this.timeout, cache: false, }); var self = this; this.client.onload = this.client.onerror = function(e) { self._onComplete(e); }; // Add this request to the queue HTTP.addToQueue(this); if (this.opt.silent !== true) { Event.trigger('http.start', { hash: this.hash, eventName: this.opt.eventName }); } // Progress callbacks if (_.isFunction(this.opt.ondatastream)) this.client.ondatastream = this.opt.ondatastream; if (_.isFunction(this.opt.ondatasend)) this.client.ondatasend = this.opt.ondatasend; // Set headers this.client.open(this.method, this.url); _.each(this.headers, function(h, k) { this.client.setRequestHeader(k, h); }); // Send the request over Internet this.startTime = new Date(); if (this.data != null) { this.client.send(this.data); } else { this.client.send(); } Ti.API.debug('HTTP: ['+this.hash+']', this.method, this.url, JSON.stringify(this.data)); };
HTTPRequest.prototype.resolve = function() { var cache = this.getCachedResponse(); if (cache != null) { if (this.onComplete !== null) this.onComplete(); if (this.onSuccess !== null) this.onSuccess(cache); } else { if (Ti.Network.online) { this.send(); } else { Ti.API.error('HTTP: connection is offline'); Event.trigger('http.offline'); if (this.onComplete !== null) this.onComplete(); this._onError({ offline: true, message: L('network_offline', 'Check your connectivity.') }); } } };
success: function(response) { Event.trigger('notifications.mute.success'); defer.resolve(response); },
.fail(function(err) { Event.trigger('auth.error', err); opt.error(err); });
load(storedDriver).logout(function(){ Event.trigger('auth.logout', { id: userID }); if (_.isFunction(callback)) callback(); });
.then(function(){ Event.trigger('auth.success', { id: Me.id }); opt.success({ id: Me.id }); })
error: function(err) { Ti.API.error('Notifications: Retrieve device token failed', err); Event.trigger('notifications.subscription.error', err); }
error: function(err) { Event.trigger('notifications.unsubscription.error', err); Ti.API.error('Notifications: Unsubscription failed to channel <' + channel + '>', err); }
exports.trigger = function(name, data) { Event.trigger(MODULE_NAME + '.' + name, data); };
.fail(function(err) { opt.error(err); if (opt.silent != true) { Event.trigger('auth.error', err); } });
error: function(err) { Event.trigger('notifications.mute.error'); defer.reject(err); }
success: function(){ Event.trigger('notifications.unsubscription.error', { channel: channel }); Ti.API.debug('Notifications: Unsubscription to channel <' + channel + '> succeded'); },
Ti.App.addEventListener('pause', function(){ pauseURL = launchURL; Event.trigger('app.paused'); });