Author: pyaschenko
Date: 2010-06-10 10:17:06 -0400 (Thu, 10 Jun 2010)
New Revision: 17592
Modified:
root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces-event.js
root/core/trunk/impl/src/test/resources/javascript/richfaces-event-qunit.js
Log:
Event API changes: refactoring, multiple binding was added to bindById method, used in
autocomplete component
QUnit tests was fixed after the changes
Modified: root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces-event.js
===================================================================
---
root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces-event.js 2010-06-10
13:44:40 UTC (rev 17591)
+++
root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces-event.js 2010-06-10
14:17:06 UTC (rev 17592)
@@ -2,6 +2,9 @@
* @author Pavel Yaschenko
*/
+// TODO: add support to bind multiple events using type param as an object with
eventType,function pairs // see bindById method
+// TODO: update js docs
+
(function($, richfaces) {
/**
@@ -27,13 +30,27 @@
return element;
}
+ var getHandlerWrapper = function (component, fn) {
+ return f = function (e,d){
+ fn.call(component||this, e, this, d);
+ };
+ }
+
+ var getMultipleHandlerWrapper = function (object, component) {
+ var result = {};
+ for (var type in object) {
+ result[type] = getHandlerWrapper(component, object[type]);
+ }
+ return result;
+ }
+
$.extend(richfaces.Event, {
/**
* @constant
* @name RichFaces.Event.RICH_NAMESPACE
* @type string
* */
- RICH_NAMESPACE : "RICH:",
+ RICH_NAMESPACE : "RICH",
/**
* Attach an event handler to execute when the DOM is fully loaded.
@@ -65,12 +82,10 @@
* It is a context for an event handler
* @return {function} function that binded to the element's event
* */
- bind : function(selector, type, fn, data) {
+ bind : function(selector, type, fn, component, data) {
// type: namespace can be used, like onclick.rf.conponentName
- var f = function (e,d){
- e.data.fn.call(e.data.component||this, e, this, d);
- };
- getEventElement(selector).bind(type, {component: data, fn:fn}, f);
+ var f = getHandlerWrapper(component, fn);
+ getEventElement(selector).bind(type, data, f);
return f;
},
@@ -86,12 +101,15 @@
* It is a context for an event handler
* @return {function} function that binded to the element's event
* */
- bindById : function(id, type, fn, data) {
+ bindById : function(id, type, fn, component, data) {
// type: namespace can be used, like onclick.rf.conponentName
- var f = function (e,d){
- e.data.fn.call(e.data.component||this, e, this, d);
- };
- $(document.getElementById(id)).bind(type, {component: data, fn:fn}, f);
+ if (typeof type == "object") {
+ // in this case fn == component object
+ $(document.getElementById(id)).bind(getMultipleHandlerWrapper(type, fn), data);
+ } else {
+ var f = getHandlerWrapper(component, fn);
+ $(document.getElementById(id)).bind(type, data, f);
+ }
return f;
},
@@ -108,12 +126,10 @@
* It is a context for an event handler
* @return {function} function that binded to the element's event
* */
- bindOne: function(selector, type, fn, data) {
+ bindOne: function(selector, type, fn, component, data) {
// type: namespace can be used, like onclick.rf.conponentName
- var f = function (e,d){
- e.data.fn.call(e.data.component||this, e, this, d);
- };
- getEventElement(selector).one(type, {component: data, fn:fn}, f);
+ var f = getHandlerWrapper(component, fn);
+ getEventElement(selector).one(type, data, f);
return f;
},
@@ -130,12 +146,10 @@
* It is a context for an event handler
* @return {function} function that binded to the element's event
* */
- bindOneById: function(id, type, fn, data) {
+ bindOneById: function(id, type, fn, component, data) {
// type: namespace can be used, like onclick.rf.conponentName
- var f = function (e,d){
- e.data.fn.call(e.data.component||this, e, this, d);
- };
- $(document.getElementById(id)).one(type, {component: data, fn:fn}, f);
+ var f = getHandlerWrapper(component, fn);
+ $(document.getElementById(id)).one(type, data, f);
return f;
},
Modified: root/core/trunk/impl/src/test/resources/javascript/richfaces-event-qunit.js
===================================================================
--- root/core/trunk/impl/src/test/resources/javascript/richfaces-event-qunit.js 2010-06-10
13:44:40 UTC (rev 17591)
+++ root/core/trunk/impl/src/test/resources/javascript/richfaces-event-qunit.js 2010-06-10
14:17:06 UTC (rev 17592)
@@ -20,12 +20,10 @@
var domElement = element;
var callback = function (e, o, d) {
- expect(6);
+ expect(4);
equals(e.type, "click");
equals(o, element);
equals(d, testData2);
- equals(e.data.component, testData1);
- equals(typeof e.data.fn, "function");
equals(this, testData1);
};
return callback;