Skip to content

Instantly share code, notes, and snippets.

@mattapperson
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattapperson/cc16490a95da097d6877 to your computer and use it in GitHub Desktop.
Save mattapperson/cc16490a95da097d6877 to your computer and use it in GitHub Desktop.
CommonJS Controllers for alloy
task('pre:compile', function(config, logger){
var wrench = require('wrench'),
fs = require('fs'),
path = require('path');
var alloyTemplates = wrench.readdirSyncRecursive(config.dir.template),
alloyTemplateDir = config.dir.template,
projectTemplateDir = path.join(config.dir.home, 'template'),
projectTemplates = wrench.readdirSyncRecursive(projectTemplateDir);
alloyTemplates.forEach(function(file) {
if(projectTemplates.indexOf(file) === -1) {
fs.symlinkSync(path.join(alloyTemplateDir, file), path.join(projectTemplateDir, file));
}
});
config.dir.template = projectTemplateDir;
});
var Alloy = require('alloy'),
Backbone = Alloy.Backbone,
_ = Alloy._;
<%= WPATH %>
function __processArg(obj, key) {
var arg = null;
if (obj) {
arg = obj[key] || null;
delete obj[key];
}
return arg;
}
function Controller() {
<%= Widget %>
require('alloy/controllers/' + <%= parentController %>).apply(this, Array.prototype.slice.call(arguments));
this.__controllerPath = '<%= controllerPath %>';
if (arguments[0]) {
var <%= parentVariable %> = __processArg(arguments[0], '<%= parentVariable %>');
var <%= modelVariable %> = __processArg(arguments[0], '<%= modelVariable %>');
var <%= itemTemplateVariable %> = __processArg(arguments[0], '<%= itemTemplateVariable %>');
}
var $ = this;
var exports = {};
var __defers = {};
// Generated code that must be executed before all UI and/or
// controller code. One example is all model and collection
// declarations from markup.
<%= preCode %>
// Generated UI code
<%= viewCode %>
// make all IDed elements in $.__views available right on the $ in a
// controller's internal code. Externally the IDed elements will
// be accessed with getView().
_.extend($, $.__views);
var ControllerObject = Controller.class.call(this, $);
var C;
if(typeof ControllerObject === 'function') {
C = new ControllerObject();
} else {
C = ControllerObject;
}
// Generated code that must be executed after all UI and
// controller code. One example deferred event handlers whose
// functions are not defined until after the controller code
// is executed.
<%= postCode %>
// Extend the $ instance with all functions and properties
// defined on the exports object.
_.extend($, exports);
}
// yes prototype is better for performance...
Controller.class = function($) {
var module = {
exports: {}
};
var exports = module.exports;
// Controller code directly from the developer's controller file
__MAPMARKER_CONTROLLER_CODE__
if(module.exports !== {}) {
return module.exports;
}
return exports;
};
module.exports = Controller;
In your Alloy project, add the alloy.jmk file to the app folder and create a directory called template, in the template directory, add the component.js file. Then simply start building your alloy apps with commonjs style controller.
Want to require in another controller? simply access it like this:
var otherController = require('otherController').class;
Events are now namespaced to the `C` variable for views.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment