76 lines
1.7 KiB
JavaScript
76 lines
1.7 KiB
JavaScript
|
|
||
|
|
||
|
var superGet, superInit,
|
||
|
globalEe = new EventEmitter();
|
||
|
|
||
|
function validateTypeEvent( value, name ) {
|
||
|
validateType( value, name, typeof value === "string" || value instanceof RegExp, "String or RegExp" );
|
||
|
}
|
||
|
|
||
|
function validateThenCall( method, self ) {
|
||
|
return function( event, listener ) {
|
||
|
validatePresence( event, "event" );
|
||
|
validateTypeEvent( event, "event" );
|
||
|
|
||
|
validatePresence( listener, "listener" );
|
||
|
validateTypeFunction( listener, "listener" );
|
||
|
|
||
|
return self[ method ].apply( self, arguments );
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function off( self ) {
|
||
|
return validateThenCall( "off", self );
|
||
|
}
|
||
|
|
||
|
function on( self ) {
|
||
|
return validateThenCall( "on", self );
|
||
|
}
|
||
|
|
||
|
function once( self ) {
|
||
|
return validateThenCall( "once", self );
|
||
|
}
|
||
|
|
||
|
Cldr.off = off( globalEe );
|
||
|
Cldr.on = on( globalEe );
|
||
|
Cldr.once = once( globalEe );
|
||
|
|
||
|
/**
|
||
|
* Overload Cldr.prototype.init().
|
||
|
*/
|
||
|
superInit = Cldr.prototype.init;
|
||
|
Cldr.prototype.init = function() {
|
||
|
var ee;
|
||
|
this.ee = ee = new EventEmitter();
|
||
|
this.off = off( ee );
|
||
|
this.on = on( ee );
|
||
|
this.once = once( ee );
|
||
|
superInit.apply( this, arguments );
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* getOverload is encapsulated, because of cldr/unresolved. If it's loaded
|
||
|
* after cldr/event (and note it overwrites .get), it can trigger this
|
||
|
* overload again.
|
||
|
*/
|
||
|
function getOverload() {
|
||
|
|
||
|
/**
|
||
|
* Overload Cldr.prototype.get().
|
||
|
*/
|
||
|
superGet = Cldr.prototype.get;
|
||
|
Cldr.prototype.get = function( path ) {
|
||
|
var value = superGet.apply( this, arguments );
|
||
|
path = pathNormalize( path, this.attributes ).join( "/" );
|
||
|
globalEe.trigger( "get", [ path, value ] );
|
||
|
this.ee.trigger( "get", [ path, value ] );
|
||
|
return value;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
Cldr._eventInit = getOverload;
|
||
|
getOverload();
|
||
|
|
||
|
return Cldr;
|
||
|
|