Myna includes

0 views
Skip to first unread message

Tony Zakula

unread,
Oct 19, 2010, 10:03:50 AM10/19/10
to mynajs-general
Consider the following:

  var actionFile = new Myna.File(actionPath);
  if (actionFile.exists()) {
    action.object = Myna.include(actionFile.toString(),{});
    action.exists = true;
  }

The include file would just contain functions and variables as discussed before on the list.

Question is, if I wanted to include another file that would have more functions I wanted to appear on the same object, would I use the same include statement?

Does that make sense?

Thanks!

Tony

Tony Zakula

unread,
Oct 19, 2010, 10:09:29 AM10/19/10
to mynajs-general
Or I guess it may be 

action.object = Myna.include(actionFile.toString(),action.object);

Mark Porter

unread,
Oct 19, 2010, 12:12:15 PM10/19/10
to mynajs-...@googlegroups.com
That would work, but you might consider something like this:

//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



--
You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
To post to this group, send email to mynajs-...@googlegroups.com.
To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.

Tony Zakula

unread,
Oct 19, 2010, 1:50:40 PM10/19/10
to mynajs-...@googlegroups.com
Okay, cool.  Building on that idea.  I am trying to make it simple for someone to override the whole object, or just one function in the custom file.  I have that working.  Looking at the ideas below, can we make it more OOP for the user?  if you were going to "subclass", you would use prototype   If a user wanted a function/method to do everything, but wanted to add one thing to it, in Java you would subclass and use the super keyword.  Using the Myna.include, have any ideas for how to make it really simple?  By using Myna.include, I have the user isolated from any subclassing as the framework is doing it for them by them placing custom files in a specific location.  I just do not want them to have to be exposed to the process.  Maybe I need an internal chain?  Of course the user could always do a Myna.include in their version of the object, unless the app engine had a specific managed chain, but then that gets away from just fast access includes.

Thanks!

TonyZ     

Tony Zakula

unread,
Oct 19, 2010, 4:39:23 PM10/19/10
to mynajs-...@googlegroups.com
On second thought, maybe I am just making it too complicated.  They could always call their function something else and then just call the base function.  The framework will always call one function, but if a custom class exists, it could call one special function which they would have free reign to do with what they want. I think this will work fine and it will be super simple.

Mark Porter

unread,
Oct 19, 2010, 5:54:40 PM10/19/10
to mynajs-...@googlegroups.com
What if the user wants to override a built-in function that your framework is calling? If you really wanted to be clever, you could create a "base" property and keep a copy of the base functions there, like so:

var actions={}
new Myna.File("classes/actions/base").listFiles("sjs").forEach(function(file){
   var name =file.fileName.listFirst(".");
   actions[name] = {}    
   //also store the functions in a "base" property
   actions[name].base=Myna.include(file,actions[name]);
})
new Myna.File("classes/actions/override").listFiles("sjs").forEach(function(file){
   var name =file.fileName.listFirst(".")
   if (name in actions){
       //any overridden properties are still in the "base" object
       Myna.include(file,actions[name]);
   } else {
       actions[name]=Myna.include(file,{});
   }
})

//an overridden action function...
function doStuff(arg1){
    if (arg1 == "Some Special Value"){
        return "custom return";
    } else {
        return this.base.doStuff.call(this,arg1);
    }
}


That might be more intuitive for the developer. Alternative names for "base" might be "core" or "super" if want be Java-like. If you wanted you could bind the "base" functions to the action object so that you don't have to use "call" or "apply"

Tony Zakula

unread,
Oct 20, 2010, 9:59:45 PM10/20/10
to mynajs-...@googlegroups.com
Cool.  I implemented something like this.  I hope to have some preliminary work ready soon.  :-)
Reply all
Reply to author
Forward
0 new messages