|
Package
Package is a prototype class that creates namespaces and imports classes and other javascript files.
Featured, Phase-Implementation OverviewPackage 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. UsageInclude 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> ExamplesCreate 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 Methodsnamespace(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 Methodsinclude(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. NoteThe methods require and include check to see if file/class is already loaded before attempting to load them again. Options
CompatibilityPackage has been tested with Safari 3, Firefox 3, IE7, and Opera. RequirementsPackage requires Prototype 1.6 | ||||||||||||||||||||||||||||||||||||