I recently discovered the OpenJSAN network, and I am impressed by the
potential! Already, I am writing modules using the JSAN-style. Thank
you David Flanagan for pushing the idea in his recent edition of
O'Reilly's "JavaScript".
This post regards the JSAN.js module specially, written by Casey West.
Chapter 10 of David's book has a module called "Module.js" that works
similar to JSAN.js. There are a couple of things I find useful in his
module but missing from yours. If these features are available in
other JSAN modules, please accept my apologies in advance; I mean no
criticism of your good work.
There is a method called "createNamespace" that is very useful. Is
there a reason this was excluded from JSAN.js?
I find myself beginning modules with the following code:
---
JSAN.use("Module") ;
Module.createNamespace("module_name_here") ;
---
Would it not be nicer to write...?
---
JSAN.createNamespace("module_name_here") ;
---
Let me know what you think.
David' original code:
---
/**
* This function creates and returns a namespace object for the
* specified name and does useful error checking to ensure that the
* name does not conflict with any previously loaded module. It
* throws an error if the namespace already exists or if any of the
* property components of the namespace exist and are not objects.
*
* Sets a NAME property of the new namespace to its name.
* If the version argument is specified, set the VERSION property
* of the namespace.
*
* A mapping for the new namespace is added to the Module.modules
object
*/
Module.createNamespace = function(name, version) {
// Check name for validity. It must exist, and must not begin or
// end with a period or contain two periods in a row.
if (!name) throw new Error("Module.createNamespace(): name
required");
if (name.charAt(0) == '.' ||
name.charAt(name.length-1) == '.' ||
name.indexOf("..") != -1)
throw new Error("Module.createNamespace(): illegal name: " +
name);
// Break the name at periods and create the object hierarchy we
need
var parts = name.split('.');
// For each namespace component, either create an object or ensure
that
// an object by that name already exists.
var container = Module.globalNamespace;
for(var i = 0; i < parts.length; i++) {
var part = parts[i];
// If there is no property of container with this name, create
// an empty object.
if (!container[part]) container[part] = {};
else if (typeof container[part] != "object") {
// If there is already a property, make sure it is an
object
var n = parts.slice(0,i).join('.');
throw new Error(n + " already exists and is not an
object");
}
container = container[part];
}
// The last container traversed above is the namespace we need.
var namespace = container;
// It is an error to define a namespace twice. It is okay if our
// namespace object already exists, but it must not already have
a
// NAME property defined.
if (namespace.NAME) throw new Error("Module "+name+" is already
defined");
// Initialize name and version fields of the namespace
namespace.NAME = name;
if (version) namespace.VERSION = version;
// Register this namespace in the map of all modules
Module.modules[name] = namespace;
// Return the namespace object to the caller
return namespace;
}
---
The full code can be found here: http://examples.oreilly.com/jscript5/
Look for module "Module.js" inside the packages.
Thanks and keep up the good work,
Kevin
> There is a method called "createNamespace" that is very useful. Is
> there a reason this was excluded from JSAN.js?
JSAN is all about loading a module and handling exports. It's not about
class or module creation.
> I find myself beginning modules with the following code:
> ---
> JSAN.use("Module") ;
> Module.createNamespace("module_name_here") ;
> ---
>
> Would it not be nicer to write...?
> ---
> JSAN.createNamespace("module_name_here") ;
> ---
I think this belongs in a separate module, really. Of course, JSAN does
define a _makeNamespace method internally, but AFAICT it won't do anything
if a module author defines their namespace internally. In fact, I'm not
even sure what the point of that code is.
Personally, I'd rather see module-helper utilities in a separate
namespace, like Module for example.
JSAN.use("Module");
var class = Module.createNamespace("My.New.Namespace");
-dave
/*===================================================
VegGuide.Org www.BookIRead.com
Your guide to all that's veg. My book blog
===================================================*/
> Personally, I'd rather see module-helper utilities in a separate
> namespace, like Module for example.
>
> JSAN.use("Module");
> var class = Module.createNamespace("My.New.Namespace");
Won't jQuery be providing something like this as it's ported to JSAN?
Best,
David
--John
> Don't think so - we don't provide any native OO hooks. I know that
> Prototype and Dojo include some - but I don't know if the have this
> in-depth namespace creation. Although, if I had to guess, I'd bet that
> Dojo does.
Good to know.
And welcome to our friendly, quiet little forum, John.
Best,
David