Simple Listener Question

View: New views
3 Messages — Rating Filter:   Alert me  

Simple Listener Question

by Adam Steffen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not experienced with javascript at all so bear with me. I'm trying to make a Web Interface with many buttons, check boxes, selection lists, etc. and associate with these widgets a set of data.  So that changing the selections on the interface update the data set.

Using qooxdoo, I made a class to hold the data, and added member functions that will update the data set, based on passed variables.  I then created an interface window with all the widgets. What I want to do is associate the member functions of the data set with listeners of the corresponding widgets.  My simple brain would like to do the following:

In window interface class:
 ...
    mycheckboxgroup.addListener("changeSelection", myDataSet.updateFunction(data_name,mycheckboxgroup), myDataSet);
 ...


Then in data set class:
 ...
    updateFunction : function(data_name,checkbox)
    {
        value = checkbox.getSelected().getValue();
        this.setData(data_name,value);
     }
 ...


This implimentation just executes the updateFunction once as the listener is added.  Based on what I've read, this is because the function argument should be a pointer to a function rather than a function() call.  But then how do you pass arguments to the Listener as it is constructed?

Is there a way to create a generic listener function that takes variable arguments?  I don't want to have to write the code for each new listener function for each of the 50 checkboxgroups i have, but I want to add a listner to each and associate that listener to a data set based on a variable.
   

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@...
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Re: Simple Listener Question

by thron7-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Adam Steffen wrote:
I'm not experienced with javascript at all so bear with me. I'm trying to make a Web Interface with many buttons, check boxes, selection lists, etc. and associate with these widgets a set of data.  So that changing the selections on the interface update the data set.

Using qooxdoo, I made a class to hold the data, and added member functions that will update the data set, based on passed variables.  I then created an interface window with all the widgets. What I want to do is associate the member functions of the data set with listeners of the corresponding widgets.  My simple brain would like to do the following:

In window interface class:
 ...
    mycheckboxgroup.addListener("changeSelection", myDataSet.updateFunction(data_name,mycheckboxgroup), myDataSet);
 ...


Then in data set class:
 ...
    updateFunction : function(data_name,checkbox)
    {
        value = checkbox.getSelected().getValue();
        this.setData(data_name,value);
     }
 ...


This implimentation just executes the updateFunction once as the listener is added.  Based on what I've read, this is because the function argument should be a pointer to a function rather than a function() call.

Right, the second argument has to evaluate to a function object, to be precise.

  But then how do you pass arguments to the Listener as it is constructed?

You don't. The listener function is monadic, taking an event object instance as a single argument. This is the listener protocol you have to adhere to. You say something like
  
   ...addListener("event", function (e) { var d = e.getData(); ....}, obj);

e will be provided at run time and is all you get for the listener invocation. The type of e can vary according to the class and event you're registering with, so check the API for that.


Is there a way to create a generic listener function that takes variable arguments?  I don't want to have to write the code for each new listener function for each of the 50 checkboxgroups i have, but I want to add a listner to each and associate that listener to a data set based on a variable.

You can write a listener factory like

    function makeListener (specialArg) {
           return function (e) { /* use event e and specialArg here */ };
    }

and then call it when you add a listener

    ...addListener("event", makeListener(specialArg), obj);

HTH,

T.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@...
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Re: Simple Listener Question

by Alexander Steitz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Adam,

first of all: welcome to the qooxdoo project!

On Friday 06 November 2009 Adam Steffen wrote:

> Using qooxdoo, I made a class to hold the data, and added member functions
> that will update the data set, based on passed variables.  I then created
>  an interface window with all the widgets. What I want to do is associate
>  the member functions of the data set with listeners of the corresponding
>  widgets.  My simple brain would like to do the following:
>
> In window interface class:
> * ...
>     mycheckboxgroup.addListener("changeSelection",
> myDataSet.updateFunction(data_name,mycheckboxgroup), myDataSet);
>  ...*
>
> Then in data set class:
> * ...
>     updateFunction : function(data_name,checkbox)
>     {
>         value = checkbox.getSelected().getValue();
>         this.setData(data_name,value);
>      }
>  ...*
>
> Is there a way to create a generic listener function that takes variable
> arguments?  I don't want to have to write the code for each new listener
> function for each of the 50 checkboxgroups i have, but I want to add a
> listner to each and associate that listener to a data set based on a
> variable.
I assume that the variable "data_name" holds data which is connected to each
checkbox group.

You can use the following snippet to achieve this

--snip--
mycheckboxgroup.setUserData("data_name", "your_data");
mycheckboxgroup.addListener("changeSelection", myDataSet.updateFunction,
myDataSet);

...

updateFunction : function(e)
{
  var checkbox = e.getTarget();
  var data_name = checkbox.getUserData("data_name");
  value = checkbox.getSelected().getValue();
  this.setData(data_name,value);
}
--snip--

This way you can use a single update method.

I hope, I understood your problem right :)

cheers,
  Alex

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@...
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel