Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_.isFunction(params.success) && params.success(resp);
} else {
// error or offline - save & use local data
// save data locally when server returned an error
if (!_response.localOnly && params.disableSaveDataLocallyOnServerError) {
params.returnErrorResponse && _.isFunction(params.error) && params.error(_response);
logger(DEBUG, "NOTICE: The data is not being saved locally");
} else {
resp = saveData();
if (_.isUndefined(_response.offline)) {
// error
_.isFunction(params.error) && params.error(params.returnErrorResponse ? _response : resp);
} else {
//offline - still a data success
_.isFunction(params.success) && params.success(resp);
}
}
}
});
break;
else if (method === 'create' || method === 'update') {
if (!model.id) {
model.id = guid();
model.set(model.idAttribute, model.id);
}
TAP.setObject(prefix + '-' + model.id, model.toJSON() || {});
resp = model.toJSON();
} else if (method === 'delete') {
TAP.removeProperty(prefix + '-' + model.id);
model.clear();
resp = model.toJSON();
}
// process success/error handlers, if present
if (resp) {
if (_.isFunction(opts.success)) { opts.success(resp); }
if (method === "read") { model.trigger("fetch"); }
} else {
if (_.isFunction(opts.error)) { opts.error(resp); }
}
}
model.set(model.idAttribute, model.id);
}
TAP.setObject(prefix + '-' + model.id, model.toJSON() || {});
resp = model.toJSON();
} else if (method === 'delete') {
TAP.removeProperty(prefix + '-' + model.id);
model.clear();
resp = model.toJSON();
}
// process success/error handlers, if present
if (resp) {
if (_.isFunction(opts.success)) { opts.success(resp); }
if (method === "read") { model.trigger("fetch"); }
} else {
if (_.isFunction(opts.error)) { opts.error(resp); }
}
}
} else direction = 1;
db = Ti.Database.open(config.adapter.db_name);
migrator.db = db;
db.execute("BEGIN;");
if (migrations.length) for (var i = 0; migrations.length > i; i++) {
var migration = migrations[i], context = {};
migration(context);
if (direction) {
if (context.id > targetNumber) break;
if (currentNumber >= context.id) continue;
} else {
if (targetNumber >= context.id) break;
if (context.id > currentNumber) continue;
}
var funcName = direction ? "up" : "down";
_.isFunction(context[funcName]) && context[funcName](migrator);
} else migrator.createTable(config);
db.execute("DELETE FROM migrations where model = ?", config.adapter.collection_name);
db.execute("INSERT INTO migrations VALUES (?,?)", targetNumber, config.adapter.collection_name);
db.execute("COMMIT;");
db.close();
migrator.db = null;
}
};
var config = (model ? model.prototype.config : {}) || {};
var mod;
if (config.adapter && config.adapter.type) {
mod = require("alloy/sync/" + config.adapter.type);
extendObj.sync = function(method, model, opts) {
mod.sync(method, model, opts);
};
} else extendObj.sync = function(method, model) {
Ti.API.warn("Execution of " + method + "#sync() function on a collection that does not support persistence");
Ti.API.warn("model: " + JSON.stringify(model.toJSON()));
};
var Collection = Backbone.Collection.extend(extendObj);
Collection.prototype.config = config;
_.isFunction(modelDesc.extendCollection) && (Collection = modelDesc.extendCollection(Collection) || Collection);
mod && _.isFunction(mod.afterCollectionCreate) && mod.afterCollectionCreate(Collection);
return Collection;
};
model: model
};
var config = (model ? model.prototype.config : {}) || {};
var mod;
if (config.adapter && config.adapter.type) {
mod = require("alloy/sync/" + config.adapter.type);
extendObj.sync = function(method, model, opts) {
return mod.sync(method, model, opts);
};
} else extendObj.sync = function(method, model) {
Ti.API.warn("Execution of " + method + "#sync() function on a collection that does not support persistence");
Ti.API.warn("model: " + JSON.stringify(model.toJSON()));
};
var Collection = Backbone.Collection.extend(extendObj);
Collection.prototype.config = config;
_.isFunction(modelDesc.extendCollection) && (Collection = modelDesc.extendCollection(Collection) || Collection);
mod && _.isFunction(mod.afterCollectionCreate) && mod.afterCollectionCreate(Collection);
return Collection;
};
db.close();
return model.toJSON();
}();
break;
case "read":
var sql = opts.query || "SELECT * FROM " + table;
db = Ti.Database.open(dbName);
var rs;
rs = _.isString(sql) ? db.execute(sql) : db.execute(sql.statement, sql.params);
var len = 0;
var values = [];
while (rs.isValidRow()) {
var o = {};
var fc = 0;
fc = _.isFunction(rs.fieldCount) ? rs.fieldCount() : rs.fieldCount;
_.times(fc, function(c) {
var fn = rs.fieldName(c);
o[fn] = rs.fieldByName(fn);
});
values.push(o);
len++;
rs.next();
}
rs.close();
db.close();
model.length = len;
resp = 1 === len ? values[0] : values;
break;
case "delete":
var sql = "DELETE FROM " + table + " WHERE " + model.idAttribute + "=?";
db = Ti.Database.open(config.adapter.db_name);
migrator.db = db;
db.execute("BEGIN;");
if (migrations.length) for (var i = 0; i < migrations.length; i++) {
var migration = migrations[i];
var context = {};
migration(context);
if (direction) {
if (context.id > targetNumber) break;
if (context.id <= currentNumber) continue;
} else {
if (context.id <= targetNumber) break;
if (context.id > currentNumber) continue;
}
var funcName = direction ? "up" : "down";
_.isFunction(context[funcName]) && context[funcName](migrator);
} else migrator.createTable(config);
db.execute("DELETE FROM migrations where model = ?", config.adapter.collection_name);
db.execute("INSERT INTO migrations VALUES (?,?)", targetNumber, config.adapter.collection_name);
db.execute("COMMIT;");
db.close();
migrator.db = null;
}
model: model
};
var config = (model ? model.prototype.config : {}) || {};
var mod;
if (config.adapter && config.adapter.type) {
mod = require("alloy/sync/" + config.adapter.type);
extendObj.sync = function(method, model, opts) {
mod.sync(method, model, opts);
};
} else extendObj.sync = function(method, model) {
Ti.API.warn("Execution of " + method + "#sync() function on a collection that does not support persistence");
Ti.API.warn("model: " + JSON.stringify(model.toJSON()));
};
var Collection = Backbone.Collection.extend(extendObj);
Collection.prototype.config = config;
_.isFunction(modelDesc.extendCollection) && (Collection = modelDesc.extendCollection(Collection) || Collection);
mod && _.isFunction(mod.afterCollectionCreate) && mod.afterCollectionCreate(Collection);
return Collection;
};
function parseJSON(_response, parentNode) {
var data = _response.responseJSON;
if (!_.isUndefined(parentNode)) {
data = _.isFunction(parentNode) ? parentNode(data, opts) : traverseProperties(data, parentNode);
}
logger(DEBUG, "server response: ", data);
return data;
}