Documentation

Function package

SYNOPSIS
Package(name[, dependencies, callback])
name
package name. Required. [String]
EG: "mypackage" or "my.common.lib".
dependencies
Array with names of packages that your package depends on. Optional. [Array]
EG: ["lib1", "lib2"]
callback
Function to be called when the dependencies have been resolved.
Optional, required when dependencies are specified. [Function]
EG: function(import1, import2) { /* inline function */ }
imports
You can import objects into the local scope by defining arguments
for the callback function with names matching those of objects
exported by packages in your dependencies list.
EXAMPLES

Example 1 (registration only)

// code that needs to be in the global namespace
Package("my.package");

Example 2 (no dependencies)

// recommended form for code without dependencies
Package("my.package", [], function() {
  // code here is in a local scope
  function greet(){ alert("hello"); }
  Export(greet); // make available to other packages
});

Example 3 (import dependencies)

Package("other.package", ["my.package"], function(greet) {
  // Use greet from my.package here.
  greet(); // alerts 'hello'
});

Function Export

SYNOPSIS

Export(obj, [name])
obj
Object to export for use in other packages. Required. [Any]
EG: Cat or myFunction
name
Name of the object to export. Required. [String]
EG: "Cat" or "example.animals.Cat"
A fully qualified name consists of the package name and the object
name separated by a dot. This form can always be used and is convenient
for overriding the default location of the exported object, or for
using this function from outside the context of a package callback.
Short names or no name at all can only be used if this function is
called from within the context of an executing package callback function
(i.e. from within your package). The object will be exported to your
package’s scope. This is the recomended form.
When the name argument is not supplied, the one-class-per-file
form is assumed to be used. In this case the object is exported AT
the package instead of IN it.
EXAMPLES

An object, function or class exported in one package…

Package ("example.animals", ["lang.Class"], function(Class) {
  var Cat = Class.create(..);
  Export(Cat, "Cat");
});

…becomes available to other packages in the global scope, under it’s
fully qualified name:

Package("some.package", ["example.animals"], function() {
  var Cat = example.animals.Cat;
  var felix = new Cat("Felix");
});

…but can also be injected in the local scope by Packages JS:

Package("other.package", ["example.animals"], function(Cat) {
  var felix = new Cat("Felix");
});

In the code above, note how Cat is injected as an argument to the package
callback function.

If you leave out the optional name argument, the object will be exported
AT the package scope instead of IN it. This variation can only be used
from within the context of a package callback, and only once per package
(obviously). This syntax is convenient for creating a one-class-per-file
package structure:

Package("example.animals.Dog",["lang.Class"], function(Class){
  var Dog = Class.Create({bark: function(){alert("woof");} });
  Export(Dog); // no name param
});

In the above example, Dog becomes available at example.animals.Dog,
not at example.animals.Dog.Dog, but can still be imported in the same
way as other exported objects:

Package("any.package", ["example.animals.Dog"], function(Dog){
  var lassie = new Dog("Lassie");
  lassie.bark(); // alerts 'woof'
});

One response to “Documentation

  1. Mark Bjerke

    Hi, this is awesome work thanks so much.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s