Level2 Base
Provides the base class and other bits of code that were being re-used within level2 projects.
Class
The Class
class provides a simple way of defining new classes and extending classes.
All uses of Class
must extend Class
itself or one of its subclasses.
var Something = Class.extend({
// Provide prototype here
});
var ChildSomething = Something.extend({
// Override Something prototoype if needed
});
ℹ️ Within override methods a special function, this._super
, is available
to call the super class's function (note that this is only defined in the child function
and is removed after the method returns)
To provide a constructor use the init
property of the prototype.
This function will be called when an instance is constructed.
var Something = Class.extend({
init: function() {
// Do initialization here
}
});
Mixins are also supported when using Class.extend()
.
var Something = Class.extend(Mixin1, Mixin2, {
// Provide prototype here
});
Function
Class.extend([mixins...], prototype) - Extends a Class
with a new prototype.
If any mixins are provided their attributes will be copied to the new class's prototype.
-
[mixins...]
Function
: mixins to apply to this class -
prototype
Object
: prototype to apply to this class
Function
Class.extendGeneric(baseType, [mixins...], prototype) - Extends any type, even if that type was not created from Class.extend()
.
-
baseType
Function
: the base type to extend -
[mixins...]
Function
: mixins to apply to the extended class -
prototype
Object
: prototype to apply to the extended class
Error
Built in extension of the javascript Error
class which includes an extend()
method.
Really this is just a convenience to calling Class.extendGeneric(Error)
.
new Error(message)
Constructs a new error message
-
message
String
: The error's message
EventEmitter
Because it was used so often a simple event emitter class is provided. This is intended to be used as a mixin.
EventEmitter.on(eventName, handler)
Adds an event listener for the given event name.
-
eventName
String
: The event name that the listener is waiting for -
handler
Function
: The event handler function
EventEmitter.emit(eventName, [data...])
Calls all the registered event listeners for the given event.
-
eventName
String
: The event to fire -
[data...]
Mixed
: The arguments to pass to the event handlers
Settings
Provides an interface to cache persistent data to a data file. To create a new settings file simply instantiate a Settings instance and pass the path to the file you want to create.
var newSettings = new Settings('/path/to/settings-file.json');
newSettings.set('someKey', 'someValue');
Settings.set()
is
called for the first time;
To retrieve settings values use the Settings.get()
method.
var sett = new Settings('/path/to/settings-file.json');
sett.get('some.value').then(function(someValue) {
console.log(someValue);
});
Note that keys containing dots ('.'
) will enter into objects.
If the base object does not exists undefined
will be returned as
the settings value.
new Settings(filePath, [dataInterface])
Creates a new Settings instance backed by the settings file
-
filePath
String
: The path to this settings storage file -
[dataInterface]
PersistenceInterface
: The optional interface used for serializing and de-serializing the settings data (defaults to the providedJSONInterface
)
Promise - Object
Settings.get(key) - Get the data at the key
-
key
String
: The key for the data to get
➡️ Returns a promise for the setting value
Promise
Settings.set(key, data) - Sets the data at the given key
-
key
String
: Key of the setting to set -
data
Any
: The data to store (must be JSON serializable)
➡️ Returns a promise that resolves when data is stored
String
Settings.getFilePath() - Get the file path used during initialization
➡️ Returns the settings path
PersistenceInterface
A PersistenceInterface
implementation provides a way to serialize and
de-serialize data in a generic fashion.
Originally this was added for use with the Settings
class.
String
PersistenceInterface.serialize(data) - Returns the string representation of the data
-
data
Object
: The data to serialize
➡️ Returns the serialized data
Object
PersistenceInterface.parse(raw) - Returns the parsed data
-
raw
String
: The data to be parsed
➡️ Returns the parsed data
JSONInterface
An implementation of PersistenceInterface
using the JSON specification.
new JSONInterface([options])
Create an interface using optional options
-
[options]
Object
: Objects for the interface-
[indent]
String
: The indent to use when serializing (default' '
, 4 spaces)
-
[indent]