//predefine action.object, assuming "action" already exists
action.object={}
... get an array of files from File.listFiles() ...
returnFromListFiles.forEach(actionFile){
if (actionFile.exists()) {
//include will accept an Myna.File instance also
Myna.include(actionFile,action.object); //appends to object
action.exists = true;
}
}
Subsequent calls will overwrite same-named properties. To prevent this you can change the include line to:
Myna.include(actionFile,{}).applyTo(action.object);
which will only set properties not already set. You can layer same-named functions as well such that earlier defined function are executed before more recently defined functions through the use of Object.After(), but that is obviously more complex.
Here is an example that builds an actions object by scanning a base directory and an override directory, layering override functions BEFORE base functions so that they can optionally override base behavior:
var actions={}
new Myna.File("classes/actions/base").listFiles("sjs").forEach(function(file){
actions[file.fileName.listFirst(".")]=Myna.include(file,{});
})
new Myna.File("classes/actions/override").listFiles("sjs").forEach(function(file){
var name =file.fileName.listFirst(".")
if (name in actions){
var action =actions[name];
Myna.include(file,{}).forEach(function(value,propName){
if (propname in action && typeof(action[propName]) == "function"){
action.before(propName,value);
} else {
action[propName] = value;
}
})
} else {
actions[name]=Myna.include(file,{});
}
})
An overrriden function might look like this:
function doStuff(arg1){
if (arg1 == "Some Special Value"){
arguments.callee.chain.exit("custom return")
}
//else execute the base function
}
----------------------------------------------------------
Mark Porter
Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org