web2py / py4web

Other
258 stars 127 forks source link

Flash fixture documentation is incorrect #912

Closed kszys closed 1 month ago

kszys commented 2 months ago

It seems that the documentation for the Flash fixture is lagging behind the actual code...

The documentation (https://py4web.com/_documentation/static/en/chapter-06.html#the-flash-fixture) gives an example that generally does not seem to work with the current version of utils.js.

It suggests setting up a <div id="py4web-flash"></div> while the script seems to be looking for something with id="alerts-flash". Also there does not seem to be a utils.flash() function that I could actually find...

It would be useful to update the documentation to match the code.

andreaschizzali commented 2 months ago

I also noticed some issues with the documentation of the Flash fixture, but ist is not clear to me where exactly are the causes. The function utils.flash(), seems to me, will be built after loading the sctipt utils.js. Towards the end of utils.js there is the line Q.handle_flash(); if a flash request was found (elem), this function defines Q.flash: Q.flash = function(detail) {elem.dispatchEvent(new CustomEvent('flash', {detail: detail}));}; then this function is evaluated and i think an event 'flash' is defined. Possibly utils.flash executes the event. I'm not shure about, my knowledge of js is limited. I also noticed, that the two flash examples in showcase are working. In flash.example.html the template uses Q.flash(). The generic template layout.html uses a custom element flash_alerts (not alerts-flash) with a data-Attribute data-alert. There are two diffrent mechanisms, only in case of a redirect the flash is stored in the cookie. Lately I was in vacation, so I did not complete my analisys of the issue. I also found other ways to get my flash messages, even if not in the canonical way. I tried something, but did not succed with the methods of the examples in showcase, when the actions are a little different. Perheaps someone more experienced has a clue.

kszys commented 2 months ago

Based on what the _scaffold app is doing nowadays, it should actually be something like this (which works fine, BTW):

which is completely not what is in the documentation.

Cheers, Krzysztof.

On Wed, 21 Aug 2024 at 23:51, Andreas Chizzali @.***> wrote:

I also noticed some issues with the documentation of the Flash fixture, but ist is not clear to me where exactly are the causes. The function utils.flash(), seems to me, will be built after loading the sctipt utils.js. Towards the end of utils.js there is the line Q.handle_flash(); if a flash request was found (elem), this function defines Q.flash: Q.flash = function(detail) {elem.dispatchEvent(new CustomEvent('flash', {detail: detail}));}; then this function is evaluated and i think an event 'flash' is defined. Possibly utils.flash executes the event. I'm not shure about, my knowledge of js is limited. I also noticed, that the two flash examples in showcase are working. In flash.example.html the template uses Q.flash(). The generic template layout.html uses a custom element flash_alerts (not alerts-flash) with a data-Attribute data-alert. There are two diffrent mechanisms, only in case of a redirect the flash is stored in the cookie. Lately I was in vacation, so I did not complete my analisys of the issue. I also found other ways to get my flash messages, even if not in the canonical way. I tried something, but did not succed with the methods of the examples in showcase, when the actions are a little different. Perheaps someone more experienced has a clue.

— Reply to this email directly, view it on GitHub https://github.com/web2py/py4web/issues/912#issuecomment-2303071216, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABV27XX76EEROWYRBN4LFADZSUDWFAVCNFSM6AAAAABM36YEGWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBTGA3TCMRRGY . You are receiving this because you authored the thread.Message ID: @.***>

mdipierro commented 2 months ago

😩

On Fri, Aug 23, 2024, 10:25 Krzysztof Socha @.***> wrote:

Based on what the _scaffold app is doing nowadays, it should actually be something like this (which works fine, BTW):

which is completely not what is in the documentation.

Cheers, Krzysztof.

On Wed, 21 Aug 2024 at 23:51, Andreas Chizzali @.***> wrote:

I also noticed some issues with the documentation of the Flash fixture, but ist is not clear to me where exactly are the causes. The function utils.flash(), seems to me, will be built after loading the sctipt utils.js. Towards the end of utils.js there is the line Q.handle_flash(); if a flash request was found (elem), this function defines Q.flash: Q.flash = function(detail) {elem.dispatchEvent(new CustomEvent('flash', {detail: detail}));}; then this function is evaluated and i think an event 'flash' is defined. Possibly utils.flash executes the event. I'm not shure about, my knowledge of js is limited. I also noticed, that the two flash examples in showcase are working. In flash.example.html the template uses Q.flash(). The generic template layout.html uses a custom element flash_alerts (not alerts-flash) with a data-Attribute data-alert. There are two diffrent mechanisms, only in case of a redirect the flash is stored in the cookie. Lately I was in vacation, so I did not complete my analisys of the issue. I also found other ways to get my flash messages, even if not in the canonical way. I tried something, but did not succed with the methods of the examples in showcase, when the actions are a little different. Perheaps someone more experienced has a clue.

— Reply to this email directly, view it on GitHub https://github.com/web2py/py4web/issues/912#issuecomment-2303071216, or unsubscribe < https://github.com/notifications/unsubscribe-auth/ABV27XX76EEROWYRBN4LFADZSUDWFAVCNFSM6AAAAABM36YEGWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBTGA3TCMRRGY>

. You are receiving this because you authored the thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/web2py/py4web/issues/912#issuecomment-2306568107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHLZT2RPNJHSCN2ZCUG6P3ZS3WXNAVCNFSM6AAAAABM36YEGWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBWGU3DQMJQG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

andreaschizzali commented 2 months ago

Thank you, the proposed modification of the documentation is fine, but solves only the server side flash. For client side flash messages, the documentation suggests to call utils.flash({'message': 'hello world', 'class': 'info'}); but I was not able to get it working. I tried in the template for an action with a simple <button onclick="utils.flash({'message': 'you clicked the button', 'class':'success'})">Click me</button> but has no reaction. I tried with <button onclick="Q.flash({message: 'Q: you clicked the button', class:'success'})">Q.Click me</button> and this works. It could be that I had to use the call to utils.flash() in a different way, but the documentation does not explain how.

mbelletti commented 2 months ago

Updated docs including working js example: https://github.com/web2py/py4web/pull/914

andreaschizzali commented 2 months ago

The example works for me only for server flash messages. I was not able to get client flash messages with the proposed utils.flash({'message': 'you clicked the button', 'class':'success'}) instead I had correct results using Q.flash(... as in the (working) example in showcase for flash_example_naive.py using examples/flash_example.html that contains

[[extend 'layout.html']]

<button onclick="Q.flash({message: 'you clicked the button', class:'warning'})">Click me</button>
<br/>or type something below<br/>
<input onchange="Q.flash({message: this.value, class: 'success'});this.value='';"/>

there is no trace of an util.flash(... I think that most probably either the documentation or the flash_example need a correction.

mdipierro commented 1 month ago

Thanks for fixing this!