On Mon, Sep 24, 2012 at 8:35 AM, Éric <[email protected]> wrote:

>
>  am a complete newbie in Pyjamas, and am following the book with great
> interest.
>
> Now, looking at the “writing widgets” part, I have been struggling to get
> things to work. This mostly because the PDF version of the book I had been
> using was somewhat obsolete. Mea culpa...
>
> Then I realised that the methods onClick() and moveSlider() should be
> defined on the VerticalDemoSlider class, and not on the  ControlDemo class,
> as said in the book (there is an erratum  there).
>
> Then the onClick method was called with one argument instead of 2.  The
> ‘event’ arg was missing (thank you pyjd for the stack trace...).
>
> Then, looking for ui.py, I found out that the file does not exist, and is
> a directory. I located the onBrowserEvent() method on the ClickListener
> class, in ClickListener.py.  This is another erratum.
>
>
> This is where I am stuck : If i don’t get the event, I don’t get the y
> position. Is there another way to get it or should the method on
> ClickListener be modified ?
>
> Modifying the method solved the problem, and I got the slider working (at
> least this part).  Now I don’t know if it is correct. Removing the argument
> from the call in ClickListener may have been done for a reason. So the
> question is : Is there a bug in ClickListener.py (event should be passed as
> an argument to onClick() ? or should the book be modified to take this into
> account ?
>
> Here is the code of the method I modified in ClickListener. Modifications
> are in *bold* :
>
>
> *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,event)
>                else:
>                    listener(self,event)
>        elif type == "dblclick":
>            if self._clickPreventDefault:
>                DOM.eventPreventDefault(event)
>            for listener in self._doubleclickListeners:
>                if hasattr(listener, "onDoubleClick"):
>                    listener.onDoubleClick(self,event)
>                else:
>                    listener(self,event)*
>
>
>
> Best regards,
>
> Éric
>
> --
>
>
>
>
Interesting....  I am also a relative newbie to web widgets in general as
well as Pyjs inn particular; however, onClick() can take more than two
arguments. onClick(self, sender, event): is a valid function declaration.
In most cases, the preferred method is to get a little more static, and
define a function for eah button with Button('some_button_name',
getattr('on_this_button", self)).    There is a nice, non-working example
at http://pyjs.org/book/Bookreader.html#UI%20Module  (search 'class
ButtonDemo(') which will enhance your button knowledge once you get it
working.  The way to do that is to examine the methods tree available to
Button and ButtonBase at pyjs.org/api/.    That particular example needs to
import a panel that will take multiple widgets since each button is a
widget, then it starts to work with only a little additional effort.
Obviously 'btn' won't be matched in the onClick()ecause it is out of scope,
so the "Who sent that???" is unlikely to display.

Event is important, sometimes, but so also is sender.  I have a widget or
two displaying multiple buttons, and the buttons displayed are dependent on
other data on the page.  Even when I grab sender, I am not out of the
woods.  I have to determine which button sent it in a text match, so I also
have to do str(sender.getHTML()), and on a parse of the button name, I have
a working dispatcher.

The documentation has not been the focus, and much of it is outdated.
Fortunately we have Pyjs sitting there to answer those questions we have as
well as some very helpful developers on the list.

Here is the structure of my button-driven dispatcher--it allows me to have
one weighty Pyjs-tailored page with one JSONRPC and a bunch of POSTs rather
than a six bunches of JSONRPCs.

def makebtntxt(some_button):
    return str(some_button.getHTML())

def input_response(response):
    """Javascript Object expected (dict of lists)"""
    for item in response:
        A.__dict__[item] = response[item] # This is necessary because
                                          # A.__dict__ has a readonly attr
                                          # and .update or = won't work
    return

def button_handler():
    A.clicked_text=makebtntxt(A.clicked)
    A.comparison=A.clicked_text.lower().strip()
    A.method_name=A.comparison
    if A.comparison=='add workflow' or A.comparison.startswith('insert'):
        la=AlphaListBoxsetup()
        lf=MfreqListBoxsetup()
        s = StackPanelSetup(la, lf)
        A.stackpanel = s
        A.stackpanel.setVisible(True)
        for b in A.buttons:
            b.setVisible(False)
        A.h.add(s)
    elif A.comparison.startswith("duplicate"):

        A.form_gendup=FormPanelDuplicate()
        A.form_gendup.setVisible(True)
        for b in A.buttons:
            b.setVisible(False)
        A.h.add(A.form_gendup)


    elif A.comparison.startswith("rename"):

        A.form_rename=FormPanelRename()
        for b in A.buttons:
            b.setVisible(False)
        A.form_rename.setVisible(True)
        A.h.add(A.form_rename)

    elif A.comparison.startswith("add") and "workflow" not in A.comparison:
        A.form_special=NewAdd()
        for b in A.buttons:
            b.setVisible(False)
        A.form_special.setVisible(True)
        A.h.add(A.form_special)

    elif A.comparison.startswith("delete"):
        A.form_delete=FormPanelDelete()
        for b in A.buttons:
            b.setVisible(False)
        A.form_rename.setVisible(True)
        A.h.add(A.form_delete)

    else:
        Window.alert("doing another path")
        pass


Thanks for your post as it enhanced my understanding.  Keep it up.  Pyjs is
far more robust than the documentation would lead you to believe.  I am
noted for pushing the envelope, and the only limitation I have found which
was NOT my error is one that is shared by Dojo and jQuery.  In fact, it
appears that Pyjs is far more capable of dynamic pages than those two.

Michael

-- 



Reply via email to