Example #1
0
SQLREST.prototype._push = function() {
	var self = this;

	var postponed = getSyncs(self.model);

	return _.reduce(_.map(postponed, function(row) {
		var new_model = null;
		var model_to_sync = row.model;

		if (self.model instanceof Backbone.Model) {
			new_model = self.model.clone();
		} else {
			new_model = new self.model.model();
		}

		try {
			new_model.set(JSON.parse(row.model));
		} catch(err) {
			Ti.API.error(LOGNAME + ' error while parsing model data: ', err);
		}

		return postponedSync(row.method, new_model, self.Remote, row.options)
		.then(function(response) {
			Ti.API.debug(LOGNAME + ': ' + response.message);

			removeSyncRow(row);
		});
	}), Q.when, Q())
	.catch(function(err) {
		Ti.API.error(LOGNAME + ': ' + err.message);
	});
};
Example #2
0
HTTPRequest.prototype.send = function() {
	var self = this;

	var promise = Q();
	_.each(filters, function(filter, name) {
		if (self.opt.suppressFilters == true) return;
		if (_.isArray(self.opt.suppressFilters) && self.opt.suppressFilters.indexOf(name) >= 0) return;
		
		promise = promise.then( filter.bind(null, self) );
	});

	promise
	.then(self._send.bind(self))
	.fail(function(ex) {
		Ti.API.error('HTTP: <' + self.uniqueId + '> filter rejection', ex);
		self.defer.reject(ex);
	});
};
Example #3
0
		Alloy.Globals.offline_listener = function(e) {
			if (!e.online || Alloy.Globals.offline_handling) return;
			Alloy.Globals.offline_handling = true;
			var stopped = [];
			var subscribed = Alloy.Globals.offline_models || [];

			_.reduce(_.map(subscribed, function(name) {
				var model = Alloy.createModel(name);

				return Q.promise(function(resolve, reject) {
					model._push()
					.then(resolve)
					.catch(resolve);
				});
			}), Q.when, Q())
			.finally(function() {
				Alloy.Globals.offline_handling = false;
			});
		};
Example #4
0
Router.prototype.dispatch = function(url, data) {
	Ti.API.debug('Router: dispatching <' + url + '>');

	var callbackURL = Util.parseAsXCallbackURL(url);
	callbackURL.path = callbackURL.path.replace(/\/$/g, '');

	if (callbackURL.protocol && this.config.protocol) {
		if (this.config.protocol !== callbackURL.protocol) {
			Ti.API.warn('Router: protocol mismatch');
			return false;
		}
	}

	callbackURL.data = data;

	var run = false;
	var matches = null;
	var routeDefinition = null;

	// Check the route to dispatch
	for (var i in this.routeRegistry) {
		routeDefinition = this.routeRegistry[i];

		if (_.isString(routeDefinition.key)) {
			// Regular string equals
			run = (routeDefinition.key === callbackURL.path);
		} else if (_.isRegExp(routeDefinition.key)) {
			// Regular expression complex match
			matches = callbackURL.path.match(routeDefinition.key);
			run = !!(matches);
			if (matches) matches.shift();
		} else if (_.isFunction(routeDefinition.key)) {
			// Function match
			matches = routeDefinition.key(callbackURL.path);
			run = (matches !== undefined);
		}

		if (run === true) break;
	}

	if (run === true) {
		Ti.API.debug('Router: matched on <' + routeDefinition.key + ', ' + JSON.stringify(matches) + '>');

		if (_.isFunction(routeDefinition.callback)) {

			this.stack.push(url);
			this.currentUrl = url;
			this.currentRoute = routeDefinition;

			if (routeDefinition.middlewares.length > 0) {

				routeDefinition.middlewares.reduce(function(soFar, f) {
					return soFar.then( f.bind(callbackURL) );
				}, Q(matches))
				.then(function() {
					routeDefinition.callback.apply(callbackURL, matches);
				})
				.catch(function(err) {
					Ti.API.error('Router: error during route dispatcher', err);
				});

			} else {
				routeDefinition.callback.apply(callbackURL, matches);
			}

		} else if (_.isObject(routeDefinition.callback)) {

			if (routeDefinition.callback.alias != null) {
				this.dispatch(routeDefinition.callback.alias);
			}

		}
	} else {
		Ti.API.warn('Router: no match for <' + url + '>');
	}

	return run;
};