Adding an object to the application

0 views
Skip to first unread message

Tony Zakula

unread,
Oct 7, 2010, 11:32:06 AM10/7/10
to mynajs-general
I am trying to add an object to the $application object.  In the start request, appEngine is always undefined.  Am I missing something easy?  :-)

applications.sjs
25: //--------- workflow methods -----------------------------------------
26: onApplicationStart:function(){ // run if application cache has expired
27: Myna.include("/myApp/appEngine/appEngine.sjs");
28: $application.set("appEngine",new appEngine());
29: },
30: onRequestStart:function(){ // run directly before requested file
31: var appEngine = $application.get("appEngine");
32: $req.handled = appEngine.handleRequest();
33: }

appEngine.sjs

var appEngine = {};
appEngine.handleRequest = function handle_Request() {
return false;
}

Thanks,

Tony


Mark Porter

unread,
Oct 7, 2010, 12:52:27 PM10/7/10
to mynajs-...@googlegroups.com
The problem is appEngine itself. You are treating it as an object constructor by calling "new appEngine()" but appEngine is not a function, it is a plain object. You probably want to simply store the object in the application scope, like so:

// this is just so that we don't end up creating a global appEngine
// object via the include. We pass in an empty object, include uses
// it as the scope for the include instead of the global scope
// and then returns the populated scope. Also added 
// $application.directory to avoid hard-coding the app folder name

var o=Myna.include($application.directory+"appEngine/appEngine.sjs",{})
$application.set("appEngine",o.appEngine);
NOTE: 
You can take advantage of Myna.include's scoping behavior to write your libraries like this:

---------------- appEngine.sjs ----------------------
//functions become properties on the resulting object
function handleRequest() {
  return false;
}
//this style works too
var doStuff = function(){
return "stuff done"
}
//any "var"d variable will become a property on the resulting object
var someObjectProperty = "woot!"

---------------- $application.sjs ----------------------
$application.set(
"appEngine",
Myna.include($application.directory+"appEngine/appEngine.sjs",{})
);


----------------------------------------------------------
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 7, 2010, 5:00:37 PM10/7/10
to mynajs-...@googlegroups.com
Okay.  Thanks.  I knew that was a simple error.  Too much Java on my brain I guess.  So, the wrinkle I am not sure about, although it may work, that I would like to understand is the "scope".  I am assuming that is Myna/Rhino specific?  What is the difference between global scope or the scope we are using?  What are we using?  request scope? M definition of scope is how long and how accessible that variable lives. If you want application wide data living there for everyone, wouldn't you want global scope?

Sorry, just trying to visualize how this takes place.

Thanks,

Tony

Mark Porter

unread,
Oct 7, 2010, 6:14:51 PM10/7/10
to mynajs-...@googlegroups.com
Ah, this gets a little confusing.First off, the JS scopes that we are talking about as request-local, meaning they go away at the end of the request. Normally a "scope" is the  bucket where items defined with the "var" keyword are created. This is normally the Global scope unless you are in a function, which has it's own scope and a parent scope. Code included via Myna.include is run against the Global scope by default, so any variable created inside are defined globally, not inside the function that calls Myna.include(). By passing your own scope object, you cause any variables defined in the included script to be created on that object instead. This is useful if you are worried about name conflicts in the global scope, or if you just don't want to pollute the global scope. Because even the Global scope will be torn down at the end of the request, you might want to store your objectes outside of the request in the "server" or "application" scopes. These are not real JS scopes. These are Java Hashtables that can store information between requests. That is why they are accessed via $application.get/set and $server.get/set respectively. It is also valid to include a file in onRequestStart() to declare an object in the global scope on every request. This is wicked fast and very reliable. This is how the Administrator loads it's function library "myna/administrator/myna_admin.sjs". Unfortunately this is tightly coupled which can make code re-use difficult. I'll probably change the Myna Admin library to the include-into-object style.

----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org


Tony Zakula

unread,
Oct 7, 2010, 9:04:59 PM10/7/10
to mynajs-...@googlegroups.com
Much clearer now.  Thanks!  
Reply all
Reply to author
Forward
0 new messages