Permalink
Browse files

Add Object.isEvent, minor readme changes

  • Loading branch information...
1 parent 28aeee0 commit 2528e7433b0a35df2853fa17b51a2872ec122f34 kangax committed May 19, 2008
Showing with 45 additions and 0 deletions.
  1. +20 −0 README.rdoc
  2. +25 −0 object.extensions.js
View
@@ -4,35 +4,55 @@ protolicious is a set of javascript snippets based on prototype.js
== What's inside?
+== Element (Element.Methods) extensions
+
* <tt>Element#setProperty</tt>
* <tt>Element#swapClassName</tt>
* <tt>Element#enableClassName</tt>
* <tt>Element#contains</tt>
+== Function extensions
+
* <tt>Function.K</tt>
* <tt>Function#negate</tt>
* <tt>Function#runOnce</tt>
* <tt>Function#_new</tt>
+== Array extensions
+
* <tt>Array#sum</tt>
* <tt>Array#namespace</tt>
+== Field (Form.Element.Methods) extensions
+
* <tt>Field#isBlank</tt>
* <tt>Field#present</tt>
+== Form (Form.Methods) extensions
+
* <tt>Form#unserialize</tt>
+== Event extensions
+
* <tt>Event.simulate</tt>
* <tt>Event.register</tt>
* <tt>Event.unregister</tt>
+== Cookie
+
* <tt>Cookie.set</tt>
* <tt>Cookie.get</tt>
* <tt>Cookie.unset</tt>
+== Prototype
+
* <tt>Prototype.addScript</tt>
* <tt>Prototype.addStylesheet</tt>
+== Object extensions
+
+* <tt>Object.isEvent</tt>
+
== License
protolicious is licensed under the terms of the MIT license
View
@@ -35,4 +35,29 @@ Object.directProperties = function(o) {
hop.call(o, prop) && result.push(prop);
}
return result;
+}
+
+/**
+ * Object.isEvent(object) -> Boolean
+ * - object(Any): Object to test
+ *
+ * Returns true if passed object "partially" conforms to a DOM2 Event interface
+ * The method uses somewhat naive duck-typing by verifying types of some of the properties as per:
+ *
+ * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event
+ *
+ * Most of the time, though, we want to either stop event, invoke #element method or
+ * access some specific property (.target, .currentTarget, etc.)
+ * In those cases, it would make sense to test for those particular methods (before calling them)
+ *
+ * Maybe Object.isExtendedEvent is the way to go?
+ *
+ * function toggleAll(e) {
+ * if (Object.isEvent(e)) e.stop();
+ * // $$('div.toggleable').invoke('toggle');
+ * }
+ *
+ **/
+Object.isEvent = function(object) {
+ return object && Object.isString(object.type) && Object.isBoolean(object.bubbles);
}

0 comments on commit 2528e74

Please sign in to comment.