mediawiki-extensions-Visual.../modules/ve/ve.Registry.js
Trevor Parscal a379e0f91e More {String} -> {string} conversions in documentation
Follow up for I6a7c9e8ee8f995731bc205d666167874eb2ebe23

The first pass that Timo took missed the following cases

* "{Array|String}": string is just one of the values
* "{String[]}": string is followed by [] to indicate an array of strings

Change-Id: I65e595e8d37fb624802d84af9536a2d3c5d73c7d
2013-01-08 13:02:12 -08:00

64 lines
1.5 KiB
JavaScript

/*!
* VisualEditor Registry class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Generic object factory.
*
* @abstract
* @extends ve.EventEmitter
* @constructor
*/
ve.Registry = function VeRegistry() {
// Parent constructor
ve.EventEmitter.call( this );
// Properties
this.registry = [];
};
/* Inheritance */
ve.inheritClass( ve.Registry, ve.EventEmitter );
/* Methods */
/**
* Associate one or more symbolic names with some data.
*
* @method
* @param {string|string[]} name Symbolic name or list of symbolic names
* @param {Mixed} data Data to associate with symbolic name
* @throws 'name must be a string'
*/
ve.Registry.prototype.register = function ( name, data ) {
if ( typeof name !== 'string' && !ve.isArray( name ) ) {
throw new Error( 'name must be a string or array, cannot be a ' + typeof name );
}
var i, len;
if ( ve.isArray( name ) ) {
for ( i = 0, len = name.length; i < len; i++ ) {
this.register( name[i], data );
}
} else if ( typeof name === 'string' ) {
this.registry[name] = data;
this.emit( 'register', name, data );
} else {
throw new Error( 'Name must be a string or array of strings, cannot be a ' + typeof name );
}
};
/**
* Gets data for a given symbolic name.
*
* @method
* @param {string} name Symbolic name
* @returns {Mixed|undefined} Data associated with symbolic name
*/
ve.Registry.prototype.lookup = function ( name ) {
return this.registry[name];
};