Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify defineCustomEvent by using some new 1.4 APIs.

  • Loading branch information...
commit 211b959926b23ff2c36dbd7b3e3fb4cf70eadff1 1 parent 45d68f0
@cpojer authored
Showing with 26 additions and 29 deletions.
  1. +26 −29 Source/Element.defineCustomEvent.js
View
55 Source/Element.defineCustomEvent.js
@@ -22,69 +22,66 @@ provides: Element.defineCustomEvent
var events = this.retrieve('events'),
list = (events && events[event]) ? events[event].values : null;
if (list){
- for (var i = list.length; i--;) if (i in list){
+ var i = list.length;
+ while (i--) if (i in list){
return true;
}
}
return false;
}});
-var wrap = function(custom, method, extended, name){
+var wrap = function(custom, method, extended){
method = custom[method];
extended = custom[extended];
- return function(fn, customName){
- if (!customName) customName = name;
-
- if (extended && !this.hasEvent(customName)) extended.call(this, fn, customName);
- if (method) method.call(this, fn, customName);
+ return function(fn, name){
+ if (extended && !this.hasEvent(name)) extended.call(this, fn, name);
+ if (method) method.call(this, fn, name);
};
};
-var inherit = function(custom, base, method, name){
- return function(fn, customName){
- base[method].call(this, fn, customName || name);
- custom[method].call(this, fn, customName || name);
+var inherit = function(custom, base, method){
+ return function(fn, name){
+ base[method].call(this, fn, name);
+ custom[method].call(this, fn, name);
};
};
var events = Element.Events;
Element.defineCustomEvent = function(name, custom){
-
var base = events[custom.base];
- custom.onAdd = wrap(custom, 'onAdd', 'onSetup', name);
- custom.onRemove = wrap(custom, 'onRemove', 'onTeardown', name);
+ custom.onAdd = wrap(custom, 'onAdd', 'onSetup');
+ custom.onRemove = wrap(custom, 'onRemove', 'onTeardown');
events[name] = base ? Object.append({}, custom, {
base: base.base,
- condition: function(event){
- return (!base.condition || base.condition.call(this, event)) &&
- (!custom.condition || custom.condition.call(this, event));
+ condition: function(event, name){
+ return (!base.condition || base.condition.call(this, event, name)) &&
+ (!custom.condition || custom.condition.call(this, event, name));
},
- onAdd: inherit(custom, base, 'onAdd', name),
- onRemove: inherit(custom, base, 'onRemove', name)
+ onAdd: inherit(custom, base, 'onAdd'),
+ onRemove: inherit(custom, base, 'onRemove')
}) : custom;
return this;
-
};
-var loop = function(name){
- var method = 'on' + name.capitalize();
- Element[name + 'CustomEvents'] = function(){
- Object.each(events, function(event, name){
- if (event[method]) event[method].call(event, name);
- });
- };
- return loop;
+Element.enableCustomEvents = function(){
+ Object.each(events, function(event, name){
+ if (event.onEnable) event.onEnable.call(event, name);
+ });
};
-loop('enable')('disable');
+Element.disableCustomEvents = function(){
+ Object.each(events, function(event, name){
+ if (event.onDisable) event.onDisable.call(event, name);
+ });
+};
})();
Please sign in to comment.
Something went wrong with that request. Please try again.