Simplify Emitter.inject() a bit, as we only need to handle _events in the first injection scope.

This commit is contained in:
Jürg Lehni 2014-10-21 23:52:53 +02:00
parent a39eea64e9
commit bfd3a91df0

View file

@ -114,44 +114,42 @@ var Emitter = {
statics: { statics: {
// Override inject() so that sub-classes automatically add the accessors // Override inject() so that sub-classes automatically add the accessors
// for the event handler functions (e.g. #onMouseDown) for each property // for the event handler functions (e.g. #onMouseDown) for each property
inject: function inject(/* src, ... */) { // NOTE: This needs to be defined in the first injection scope, as for
for (var i = 0, l = arguments.length; i < l; i++) { // simplicity, we don't loop through all of them here.
var src = arguments[i], inject: function inject(src) {
events = src._events; var events = src._events;
if (events) { if (events) {
// events can either be an object literal or an array of // events can either be an object literal or an array of
// strings describing the on*-names. // strings describing the on*-names.
// We need to map lowercased event types to the event // We need to map lowercased event types to the event
// entries represented by these on*-names in _events. // entries represented by these on*-names in _events.
var types = {}; var types = {};
Base.each(events, function(entry, key) { Base.each(events, function(entry, key) {
var isString = typeof entry === 'string', var isString = typeof entry === 'string',
name = isString ? entry : key, name = isString ? entry : key,
part = Base.capitalize(name), part = Base.capitalize(name),
type = name.substring(2).toLowerCase(); type = name.substring(2).toLowerCase();
// Map the event type name to the event entry. // Map the event type name to the event entry.
types[type] = isString ? {} : entry; types[type] = isString ? {} : entry;
// Create getters and setters for the property // Create getters and setters for the property
// with the on*-name name: // with the on*-name name:
name = '_' + name; name = '_' + name;
src['get' + part] = function() { src['get' + part] = function() {
return this[name]; return this[name];
}; };
src['set' + part] = function(func) { src['set' + part] = function(func) {
// Detach the previous event, if there was one. // Detach the previous event, if there was one.
var prev = this[name]; var prev = this[name];
if (prev) if (prev)
this.off(type, prev); this.off(type, prev);
if (func) if (func)
this.on(type, func); this.on(type, func);
this[name] = func; this[name] = func;
}; };
}); });
src._eventTypes = types; src._eventTypes = types;
}
inject.base.call(this, src);
} }
return this; return inject.base.apply(this, arguments);
} }
} }
}; };