Getting page object values

For questions about Gambas web tools.
Post Reply
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Getting page object values

Post by BruceSteers »

Anyone know how to communicate and get return values from the pages objects?

For example my form has a WebAudio object.
I want to read/write the DOM property .currentTime

So from my gambas cgi web app i need to do something like this...

Dim iSeconds As Integer = WebForm1.GetValue("WebAudio1", "currentTime")

Only there is no GetValue command. :(
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Getting page object values

Post by BruceSteers »

So after some play i realize the following..
(Note: these are initial findings that i could be wrong about)

There must be 2 parts to a gambas web page, the gambas part and a .js file with your functions in it (if you want fancy functions).

When the gambas application runs on the server it basically converts itself into a bunch of javascript functions.

All the gambas type WebButton1_Click() events and the like become javascript functions from the lib.js file of gb.web.gui.
This ALL happens on load and all the client browser receives is the output html/javascript code.
Any other functionality needed after page load HAS to happen in your .js file. (make a file like myfunctions.js and place it in project dir)
this requires working out how Javascript works too (luckily i have some experience)
Forget that quote, i was wrong (see later post)

Examining the code of the lib.js file from gb.web.gui and the code of the controls is a great way to learn what's happening and doing the same.

So I CAN get object values and work with them but only if i do it in my own custom.js file.

The bad thing is that errors in the js file are not so easy to find. using firefox or chrome browsers dev tools can help a lot.
Last edited by BruceSteers on Saturday 30th July 2022 6:09pm, edited 1 time in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Getting page object values

Post by BruceSteers »

Okay so for my particular task here i found the best thing was to make my own event.

Within my WebAudio.class I used something like the following...

Event TimeUpdate(Position As Float)

Private $hObserver As Observer
Private $fPosition As Float

Public Sub _new()

  $hObserver = New Observer(Me) As "This"

End

Public Sub This_TimeUpdate(Pos As Float)
  
  $fPosition = Pos
  
End


Public Sub _Render()
  
  Print "<audio"; Me._GetClassId(); " src=\""; Html(Me._GetLink($sAudio)); "\"";
  If $bLoop Then Print " loop=\"1\"";
  Print Me._GetEventJS("ontimeupdate", "TimeUpdate", "[$_(" & JS(Me.Name) & ").currentTime]")
  Print "></audio>";
  
End


Now in my WebForm i can use the following event...

Public Sub WebAudio1_TimeUpdate(Optional Position As Float)

  $fCurrentPosition = Position
  lblTime.Text = TimeToString(Position)

End

Doing this seems to work really well for various things.
i have a readable time position now in the WebAudio and also in the webform.

Hoping that'll help someone...

BruceS
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Getting page object values [solved]

Post by BruceSteers »

Okay now with some very helpful advice from Benoit I have the following answer to my issue...

firstly a problem with using an event handler the way i was...
Benoit Minisini wrote: Note that you have a WebControl '_GetUpdateJS()' method that generates
for you the HTML code that calls 'gw.update' when a specific DOM event
is triggered.
So scrap that idea for that purpose as it was causing all my objects to refresh/flicker.
Benoit Minisini wrote: You must send the 'Position' property from the browser to the server by
using:

gw.update(<id>, '<property>', <value>);

Where <id> is the id of the control, <property> the name of the
property, and <value> its value.

Then you handle that message on the server in the '_UpdateProperty'
hidden method of the control.

So this method works briliantly...

Event Position

Property Position As Float Use $fPosition

Public Sub _Render()

  Print "<audio"; Me._GetClassId(); " src=\""; Html(Me._GetLink($sAudio)); "\"";

  Print " ontimeupdate=\"gw.update(" & JS(Me.Name) & ",'position', $_(" & JS(Me.Name) & ").currentTime, null)\"";

  Print "></audio>";

End

Public Sub _UpdateProperty(sProp As String, vValue As Variant)

  If sProp = "position"
    $fPosition = vValue
    Raise Position
  Endif

End

No more flickering.
the ontimeupdate event auto-sets $fPosition and raises the Position event.


One more quote from Benoit as it's important need-to-know stuff...
Benoit Minisini wrote: When a property of a control changes, you usually must refresh it by
calling the _Refresh() method.

You can avoid a full refresh of the control to speed up things by using
custom javascript code instead. Look at the SetText() method of the
WebTextBox control to see an example.

Another point: you must not catch an event on the browser side if you
don't implement the event handler on the server side.

To detect that, use the 'Object.CanRaise()' method. As usual, look at
the '_Render' method of WebTextBox for an example.
Many thanks to Benoit for his help and hopefully this can help you too.

Bruce
If at first you don't succeed , try doing something differently.
BruceS
Post Reply