Namespaces
Fields
-
$global
- Global namespaceAssumes window within a web browser or global in nodejs.
- Source:
- module/core.js, Line: 15 ( Module: capri/core )
Methods
- $class(name, members, [staticMembers]) -> {object}
- $class(name, extends, members, [staticMembers]) -> {object}
- $class.add(class, [members], [static], [staticFilter]) -> {class}
- $clientUri(uri, [query]) -> {string}
- $include(uri, [callback], [force])
- $include.process()
- $namespace(ns, [extension]) -> {object}
- $namespace(ns, define) -> {object|undefined}
- $namespace.getBaseId(ns) -> {string}
- $namespace.getId(ns) -> {string}
- $namespace.use(ns, fn) -> {mixed}
- $startup(callback)
- <protected> $startup.process()
-
$class(name, members, [staticMembers]) -> {object}
- Define new class using the classical approachSupported Features:
- Inheritance and Polymorphism
- Object constructor
- Static members
- Static constructor
Parameters
Name Type Argument Description namestring Name of class (may optionally specify namespace)membersobject Per-instance methods, fields, etc.staticMembersobject <optional> Static methods, fields, etc.Returns:
- Type
- object
Class referenceExamples
Ex#1. Define class for product details
$class('ProductDetails', { __construct: function(type, title, price) { this.type = type; this.title = title; this.price = price; }, toString: function() { return this.type + ': ' + this.title + ' ($' + this.price + ')'; } });Ex#2. Usage of product details class
book = new ProductDetails('book', 'War of the Worlds', '4.99'); console.log(book.toString());Ex#3. Mathematical vector with usage example
$class('mygame.Vector2d', { __construct: function(x, y) { this.x = x || 0; this.y = y || 0; }, normalise: function() { var mag = this.magnitude(); this.x /= mag; this.y /= mag; }, magnitude: function() { return Math.sqrt(this.x * this.x + this.y * this.y); }, dot: function(b) { return this.x * b.x + this.y * b.y; }, smallestAngle: function(b) { // Convert from radians to degrees return Math.acos(this.dot(b)) * 57.2957795; } },{//STATIC __static: function() { // static constructor // this is alias of mygame.Vector2d this.north = new this(0, -1); } }); // Unit vector for velocity of entity vel = new mygame.Vector2d(15, 0); vel.normalise(); // Calculate angle of vector (in degrees 0-360) relative to north angle = vel.smallestAngle(mygame.Vector2d.north); angle = vel.x < 0 ? 360 - angle : angle; // angle ~ 90 degrees -
$class(name, extends, members, [staticMembers]) -> {object}
- Define new class by extending an existing class
- See
-
Parameters
Name Type Argument Description namestring Name of class (may optionally specify namespace)extendsstring | object Name or reference of classmembersobject Per-instance methods, fields, etc.staticMembersobject <optional> Static methods, fields, etc.Throws:
- Type
- capri.Error
Thrown on invalid parameterReturns:
- Type
- object
Class referenceExamples
Ex#1. Define class that represents a key entity
$class('mygame.Entity', { __construct: function(x, y) { this.x = x; this.y = y; }, process: function() {}, render: function(dc) {}, distanceTo: function(x, y) { return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2)); } }); $class('mygame.Key', 'mygame.Entity', { __construct: function(x, y, doorName) { this.callSuper(mygame.Entity, '__construct', x, y); // Name of door that key can open this.doorName = doorName; }, render: function(dc) { dc.fillStyle = 'yellow'; dc.fillRect(this.x, this.y, 5, 5); } });Ex#2. Create instance of class
key = new mygame.Key(300, 400, 'yellow'); sceneNode.push(key);Ex#3. Utilise instance method of object
d = key.distanceTo(0, 0); // 500 -
$class.add(class, [members], [static], [staticFilter]) -> {class}
- Add instance and/or static members to existing classNote: New members are also added to classes that inherit specified class.
Parameters
Name Type Argument Description classstring | class Name or reference of classmembersobject | null <optional> New instance membersstaticobject | null <optional> New static membersstaticFilterfunction <optional> Callback that is used to filter static membersReturns:
- Type
- class
Input class is returnedExample
Add new functions to existing class
$class('Test', { foo: function() { return 'hey'; } }); var obj = new Test(); obj.baz; // undefined $class.add(Test, { bar: function() { return this.baz; }, baz: 42 }); obj.baz; // 42 -
$clientUri(uri, [query]) -> {string}
- Resolve URI for use in clientGenerate URI for use in client (web browser) by replacing tilde ~ character with base URI of website. Additional query arguments can be added to URI for convenience.
- Source:
- module/doc-manager.js, Line: 18 ( Module: capri/doc-manager )
Parameters
Name Type Argument Description uristring URI that must be resolvedquerystring | object <optional> Query argumentsReturns:
- Type
- string
Resolved URIExample
// Initialise base URI of website capri.DocumentManager.setBaseUri('http://example.com/blog/'); // elsewhere... uri = $clientUri('~/about', 'section=intro'); // "http://example.com/blog/about?section=intro" uri = $clientUri('http://example.com/blog/about', 'section=intro'); // "http://example.com/blog/about?section=intro" uri = $clientUri('~/about?section=intro', 'labels=off'); // "http://example.com/blog/about?section=intro&labels=off" -
$include(uri, [callback], [force])
- Dynamically include script or stylesheetNote: Tilde ~ can be used in place of base URI of application if document base URI has been specified (see Ex#2).
- See
-
- Source:
- module/doc-manager.js, Line: 98 ( Module: capri/doc-manager )
Parameters
Name Type Argument Description uristring URIcallbackfunction | null <optional> Callback executed once script has loadedforceboolean <optional> Defaults to false.- true - force execution of script
- false - avoid execution of script if previously executed
Examples
Ex#1. Include absolute URI
$include('http://example.com/js/myscript.js');Ex#2. Include with Base URI
$include('~/js/myscript.js');Ex#3. Includes with callback
$include('~/js/first.js'); $include('~/js/second.js', function() { // Callback invoked once both 'first.js' and 'second.js' have loaded. // Will not execute if script throwns an exception during loading. });Ex#4. Force re-inclusion of previously included script
$include('~/js/render-object.js', null, true); $include('~/js/render-object.js', null, true); $include.process(); -
$include.process()
- Process queued script and stylesheet includesDynamic includes must be manually processed once initial startup has completed.
- Source:
- module/doc-manager.js, Line: 139 ( Module: capri/doc-manager )
Throws:
- Type
- capri.Error
Cannot process includes until page has finished loadingExample
Perform script inclusion
function setWindowSkin(skinName) { // Note: This is merely an example of how dynamic includes might be used! // Queue includes $include('~/skin/' + skinName + '/style.css'); $include('~/skin/' + skinName + '/skin.js', function() { // Apply velvit skin to application window $('#app-window') .applyAppSkin(skinName); }); // Process includes $include.process(); } -
$namespace(ns, [extension]) -> {object}
- Define or acquire access to namespaceNamespace will be defined if it doesn't already exist. Members can be added to a namespace by specifying extension object (see Ex#3).Web applications often make extensive use of JavaScript to provide an extensive interactive experience. Some of these scripts will be custom written whilst others may be third-party libraries. Occasionally scripts will conflict with one another due to multiple use of the same identifier.Conflicts can easily be avoided within custom written scripts by defining functions, classes and variables within a vendor specific namespace. This function aims to ease namespace usage.
Parameters
Name Type Argument Description nsstring | object Namespace to lookupextensionobject <optional> Extension members to addReturns:
- Type
- object
Namespace objectExamples
Ex#1. Access namespace
$namespace('my.special').foo();Ex#2. Ensure that namespace is defined
$namespace('my.special'); // The following would fail if "my" namespace had not been defined. Above use // of $namespace avoids ReferenceError exception. if (my.special.foo) my.special.foo();Ex#3. Add members to namespace
$namespace('my.special', { foo: function() { } });Ex#4. Access global namespace
// Easy way to access global namespace: $global.globalVar = 42; $namespace($global, { globalFunction: function() { console.log('Global function!'); } }); // Alternative: $namespace('').globalVar = 42; $namespace('', { globalFunction: function() { console.log('Global function!'); } }); -
$namespace(ns, define) -> {object|undefined}
- Acquire access to an existing namespace
Parameters
Name Type Description nsstring | object Namespace to lookupdefineboolean false prevents namespace from being definedReturns:
- Type
- object | undefined
Namespace object or undefined if namespace is not defined.Example
Find out if namespace is defined
var ns = $namespace('my.special.namespace'); if (ns !== undefined) console.log('Namespace is defined'); else console.log('Namespace is NOT defined'); -
$namespace.getBaseId(ns) -> {string}
- Get base ID of namespace
Parameters
Name Type Description nsstring Namespace to lookupReturns:
- Type
- string
Base ID as stringExample
$namespace.getBaseId('my.special.namespace'); // "my.special" -
$namespace.getId(ns) -> {string}
- Get ID of namespace
Parameters
Name Type Description nsstring Namespace to lookupReturns:
- Type
- string
ID as stringExample
$namespace.getId('my.special.namespace'); // "namespace" -
$namespace.use(ns, fn) -> {mixed}
- Use alias to access namespaceShorter aliases can be used to access namespaces within a specified scope. This avoids repeatedly entering long namespaces.
Parameters
Name Type Description nsstring Namespace to lookupfnfunction Scope to use namespace withinReturns:
Value returned by scope fnExamples
Ex#1. Use single namespace
$namespace.use('my.special.namespace', function($myns) { // Both of the following lines are equivalent: $myns.foo(); my.special.namespace.foo(); });Ex#2. Use multiple namespaces
$namespace.use([ 'my.special.namespace', jQuery ], function($myns, $) { // Both of the following lines are equivalent: $('#my-element').text($myns.foo()); jQuery('#my-element').text(my.special.namespace.foo()); }); -
$startup(callback)
- Execute callback upon startupCallback is executed once both DOM and includes have been loaded.
- Source:
- module/doc-manager.js, Line: 50 ( Module: capri/doc-manager )
Parameters
Name Type Description callbackfunction | function[] One or more callbacksExample
$include('~/css/jquery/special.css'); $include('~/js/jquery.special.js'); $startup(function() { // Special plugin is available because includes have been processed $('#special-widget').specialPlugin(); }); -
<protected> $startup.process()
- Process startup scripts explicitlyNote: Startup scripts are usually processed automatically.
- Source:
- module/doc-manager.js, Line: 72 ( Module: capri/doc-manager )
Throws:
- Type
- capri.Error
Thrown when invalid startup script is encountered

