My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 28, 2008 by xsokev
Labels: Featured, Phase-Implementation
Package  
Package is a prototype class that creates namespaces and imports classes and other javascript files.

Overview

Package is a Prototype Javascript Framework class that allows developers to dynamically create namespaces. These namespaces can be used to better organize your code. They also help to reduce the amount of code that pollutes the global namespace. There are many articles that offer more information about javascript namespaces.

Package allows developers to import existing classes (tested only with Prototype Classes) to be imported into a namespace even if the classes aren’t already namespaced. Package also allows any javascript and css file to be included dynamically without being namespaced whether local or cross domain.

Usage

Include the Package.js file on your page after prototype.js.

<script src="prototype.js" type="text/javascript" charset="utf-8"></script>
<script src="Package.js" type="text/javascript" charset="utf-8"></script>

Examples

Create namespaces.

var pkg = new Package(“com.acme”); 
pkg.namespace(“com.acme.ui”);
pkg.namespace(“com.acme.lang”, “com.acme.io”, “com.acme.util”);

Import existing classes into namespaces. The following example imports two classes and uses them if they were imported successfully.

pkg.require([“com.acme.util.Currency”, “com.acme.util.Formatter”], { success: function(){
	var fmt = new com.acme.util.Formatter();
	fmt.format(“Donate {1} please!”, new con.acme.util.Currency(“10”, “Euro”));
} });

Include existing css and javascript files. The following example includes the css and javascript files for SyntaxHighlighter then executes the command to highlight formatted markup.

Package.include([“SyntaxHighlighter.css”, “shCore.js”, “shBrushJScript.js”], { success: function(){
	dp.SyntaxHighlighter.HighlightAll('code');
} });

More examples can be found here. Package Examples

Instance Methods

namespace(namespaces) - creates namespace objects.

namespaces is a String or an Array of strings. These strings are used to create namespace objects.

require(namespaces[, options]) - imports classes into existent or non-existent namespaces.

namespaces is a String or an Array of strings. These strings are used to find the path to the classes. The path is determined by specifying the namespace with a period (e.g. com.acme.ui.String) or with a file path separator (e.g. com/acme/ui/String).
options is a object. See Require Options below.

Static Methods

include(files [, options]) - dynamically includes javascript and css files on a page.

files is a String or an Array of strings. These strings are the paths to the javascript or css files. The path can be relative or absolute.
options is a object. See Include Options below.

Note

The methods require and include check to see if file/class is already loaded before attempting to load them again.

Options

Package Options Default Description
importPath / the base path where Package should look for classes to import
extension .js the default extension Package should use when importing classes
Require Options Default Description
success the function to execute when the import has finished successfully
failure the function to execute when the import has failed
create true indicates whether the import method should create the namespace for the class(es) being imported
Include Options Default Description
success the function to execute when the include has finished successfully
failure the function to execute when the include has failed
location head indicates where the loaded script should be placed. Expects 'head' or 'body'
type the type of file being imported. This should be applied to the list of files when the extension of the file isn't obvious. If used, do not mix css and js files when calling the include method.

Compatibility

Package has been tested with Safari 3, Firefox 3, IE7, and Opera.

Requirements

Package requires Prototype 1.6


Sign in to add a comment
Hosted by Google Code