On Tue, Feb 21, 2012 at 5:31 AM, John Wong <[email protected]> wrote:
> In this tutorial, Introduction to Pyjamas, Part 1  (http://goo.gl/wNFZb),
> the write has this snippet
>
>     def onCellClicked(self, sender, row, cell):
>         self.gui_eventContactGridClicked(row, cell)
>
>     def onClick(self, sender):
>         if sender == self.addButton:
>             self.gui_eventAddButtonClicked()
>         elif sender == self.addNewButton:
>             self.gui_eventAddNewButtonClicked()
>         elif sender == self.updateButton:
>             self.gui_eventUpdateButtonClicked()
>
>
> I don't understand how onCellClicked() and onClick() get called.

ok - i'm not going to "tell" you, i'm going to go through the source
code, and show you how you can find out for yourself, ok?

right, let's assume we know nothing about onClick.  we're looking for
calls of onClick, right?  so where do you start?  in the source code
of course.

$ cd pyjamas/library
$ find . -name "*.py" | xargs grep onClick

 hmmm, that shows too much, because we're looking for *calls* of
onClick, not definitions, and not other functions.  try this instead:

$ find . -name "*.py" | xargs grep onClick | grep -v "def " | grep -v
"onClickStart" | grep -v "onClickCancel"

that gives this list:

./pyjamas/raphael/raphael.py:        onClick      = getattr(self, "_onClick")
./pyjamas/raphael/raphael.py:           this._event_element.onclick
  = @{{onClick}};
./pyjamas/builder/Builder.py:    onClick = ("addClickListener", "sender"),
./pyjamas/ui/ClickDelegatePanel.py:    # receive Label's onClick and
pass it through, pretending it came from us
./pyjamas/ui/ClickDelegatePanel.py:        self.clickDelegate.onClick(sender)
./pyjamas/ui/ClickDelegatePanel.py:            self.onClick(self)
./pyjamas/ui/ClickListener.py:                if hasattr(listener, "onClick"):
./pyjamas/ui/ClickListener.py:                    listener.onClick(self)
./pyjamas/ui/PushButton.py:        CustomButton.onClick(self)
./pyjamas/ui/CustomButton.py:                    self.onClick()
./pyjamas/ui/CustomButton.py:                self.onClick()
./pyjamas/ui/CustomButton.py:                self.onClick()
./pyjamas/ui/CustomButton.py:        DOM.buttonClick(self.getElement())
./pyjamas/ui/ToggleButton.py:        CustomButton.onClick(self)
./pyjamas/chart/GChartWidgets.py:    * can easily implement
<tt>ClickListener.onClick</tt>
./pyjamas/chart/GChart.py:    * is, an object whose
<tt>ClickHandler.onClick</tt> method will be
./pyjamas/chart/GChart.py:    *  <tt>ClickEvent</tt> passed into your
<tt>onClick</tt> handler


so, ignore CustomButton, Delegate, GChart, raphael and Builder, that
leaves.... ClickListener!

yaay, we've just found the class which is responsible for calling onClick!

let's take a look:

    def onBrowserEvent(self, event):
        """Listen to events raised by the browser and call the appropriate
        method of the listener (widget, ..) object.
        """
        type = DOM.eventGetType(event)
        if type == "click":
            if self._clickPreventDefault:
                DOM.eventPreventDefault(event)
            for listener in self._clickListeners:
                if hasattr(listener, "onClick"):
                    listener.onClick(self)
                else:
                    listener(self)

so, hmmm, that looks like if there's a browser event, of type "click",
then any listeners in the list of listeners get called.  obvious,
right?  so, hmmm, what's this class called?

class ClickHandler(object):

ok, so it would make sense therefore that anything which derives from
ClickHandler would have "click" functionality, right?


> Are those
> built-in methods of some GUI.ui.xxx class?

 evidently not.  but you could have checked for yourself by looking in
the source code.  it's not a "black-box".  it's a _small_ amount of
code that is easily understandable just by examining it and going over
it with some simple search tools.

> I see multiple onClick() methods
> from the documentation (http://pyjs.org/api/identifier-index.html), and
> onCellClicked() under the Calendar
> (http://pyjs.org/api/pyjamas.ui.Calendar.Calendar-class.html#onCellClicked)
>
> I don't see where the inheritance is coming from. It is unclear to me.

 well, we've identified, above that the class responsible is
ClickHandler.  therefore, doing "find . -name "*.py" | xargs grep
ClickHandler" will reveal the base classes which inherit from that
class.

 event handling is always a bit of a "disconnect" in all GUI toolkits, bar none.


> For the full script, you may see it here:  http://pastebin.com/BRR9EJJS
>
> Thank you. Please someone explain this to me, and advise me the best way to
> pick up the APIs.

 open every single file in library/pyjamas/ui/*.py and
library/pyjamas/*.py and do page-down, page-down, page-down.  don't
spend more than about 3 seconds per file, if that.  this will take you
about 10 mins total.  let your subconscious work for you.

 later you will go "hmmm..... i've seen that function before...
hmmm...." and then you can use find and grep to look for a function.

 also keep glancing at the file hierarchy and the class hierarchy.
there's a diagram kicking around, somewhere on the web site.

 l.

Reply via email to