Skip to content

Commit

Permalink
Merge pull request #213 from appcelerator/ALOY-351
Browse files Browse the repository at this point in the history
[ALOY-351] Add "adapters" config.json property
  • Loading branch information
tonylukasavage committed Aug 7, 2013
2 parents bf9b979 + 9f2691d commit fc61d75
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 79 deletions.
33 changes: 31 additions & 2 deletions Alloy/commands/compile/Orphanage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ var fs = require('fs'),

var ALLOY_ROOT = path.join(__dirname, '..', '..');

var dirs, platform, titaniumFolder, theme;
var dirs, platform, titaniumFolder, theme, adapters;
var widgets = {};

function Orphanage(projectDir, _platform, opts) {
opts = opts || {};
platform = _platform;
titaniumFolder = platforms[platform].titaniumFolder;
theme = opts.theme;
theme = opts.theme,
adapters = opts.adapters || [];

// gather directories to be used throughout Orphanage
var resourcesDir = path.join(projectDir, CONST.RESOURCES_DIR);
Expand All @@ -39,6 +40,10 @@ Orphanage.prototype.clean = function() {
// Clean the base app folder
this.removeAll();

// get rid of unused adapters
logger.debug('Removing orphaned sync adapters...');
this.removeAdapters();

// Clean out each widget
var widgets = path.join(dirs.runtime, CONST.DIR.WIDGET);
if (fs.existsSync(widgets)) {
Expand Down Expand Up @@ -67,6 +72,30 @@ Orphanage.prototype.removeAll = function(opts) {
this.removeStyles(opts);
};

Orphanage.prototype.removeAdapters = function(opts) {
opts = _.clone(opts || {});
var paths = [
path.join('alloy', 'sync'),
path.join(titaniumFolder, 'alloy', 'sync')
];

_.each(paths, function(p) {
var adapterDir = path.join(dirs.resources, p);
if (!fs.existsSync(adapterDir)) {
return;
}

_.each(fs.readdirSync(adapterDir), function(adapterFile) {
var fullpath = path.join(adapterDir, adapterFile);
var adapterName = adapterFile.replace(/\.js$/, '');
if (!_.contains(adapters, adapterName)) {
fs.unlinkSync(fullpath);
logger.trace('* ' + path.join(p, adapterFile));
}
});
});
};

Orphanage.prototype.removeControllers = function(opts) {
opts = _.clone(opts || {});
remove(_.extend(opts, {
Expand Down
13 changes: 12 additions & 1 deletion Alloy/commands/compile/compilerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,19 @@ exports.createCompileConfig = function(inputPath, outputPath, alloyConfig) {
autoStyle: false,

// the list of widget dependencies
dependencies: {}
dependencies: {},

// TODO: Include no adapters by default
adapters: CONST.ADAPTERS
});

// normalize adapters
if (!configs.adapters) {
configs.adapters = [];
} else if (!_.isArray(configs.adapters)) {
configs.adapters = [configs.adapters];
}

logger.debug(JSON.stringify(configs, null, ' ').split(os.EOL));

// update implicit namespaces, if possible
Expand Down
12 changes: 10 additions & 2 deletions Alloy/commands/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ module.exports = function(args, program) {

// wipe the controllers, models, and widgets
logger.debug('----- CLEANING RESOURCES -----');
var orphanage = new Orphanage(paths.project, buildPlatform, { theme: theme });
var orphanage = new Orphanage(paths.project, buildPlatform, {
theme: theme,
adapters: compileConfig.adapters
});
orphanage.clean();
logger.debug('');

Expand All @@ -107,7 +110,12 @@ module.exports = function(args, program) {
U.updateFiles(
path.join(alloyRoot, 'lib'),
path.join(paths.resources, titaniumFolder),
{ rootDir: paths.project }
{
rootDir: paths.project,
exceptions: _.map(_.difference(CONST.ADAPTERS, compileConfig.adapters), function(a) {
return path.join('alloy', 'sync', a + '.js');
})
}
);
U.updateFiles(
path.join(alloyRoot, 'common'),
Expand Down
1 change: 1 addition & 0 deletions Alloy/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports.ITEM_TEMPLATE_VAR = '__itemTemplate';
exports.PARENT_SYMBOL_VAR = '__parentSymbol';
exports.WIDGET_OBJECT = 'Widget';
exports.SKIP_EVENT_HANDLING = ['Ti.UI.ListItem','Alloy.Abstract.ItemTemplate'];
exports.ADAPTERS = ['localStorage', 'properties', 'sql'];

// property names
exports.CLASS_PROPERTY = 'classes';
Expand Down
12 changes: 8 additions & 4 deletions Alloy/lib/alloy/sync/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ function Sync(method, model, opts) {
}

model.length = len;
len === 1 ? resp = model.models[0] : resp = model.models;
if (len === 1) {
resp = model.models[0];
} else {
resp = model.models;
}
break;

case 'update':
Expand All @@ -68,10 +72,10 @@ function Sync(method, model, opts) {

// process success/error handlers, if present
if (resp) {
_.isFunction(opts.success) && opts.success(resp);
method === "read" && model.trigger("fetch");
if (_.isFunction(opts.success)) { opts.success(resp); }
if (method === "read") { model.trigger("fetch"); }
} else {
_.isFunction(opts.error) && opts.error(resp);
if (_.isFunction(opts.error)) { opts.error(resp); }
}
}

Expand Down

0 comments on commit fc61d75

Please sign in to comment.