Execute jQuery script upon #onAjaxComplete?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Execute jQuery script upon #onAjaxComplete?

Mariano Martinez Peck
Hi guys,

I am trying to make some alerts/notifications to be displayed to users. For some internal reasons, these notifications are at server side. I have main component that on the #renderContentOn: does the render of the needed notifications. 

This works fine for normal requests as any "interaction" from the user with the app, will make a normal request causing a re-render of everything, included the notification component. 

The problem, as usual, is all ajax requests (and my app is now almost everything with ajax). I need to hook on ajax calls so that I could also re-render the notifications components as there could have been new notifications since the last rendering. 

I tried the following:

html 
document 
addLoadScript: (html jQuery document
                     onAjaxComplete: (
  (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith: [:r |
self systemMessageNotifier renderContentOn: r.
]
)).

But obviously that doesn't work because the onAjaxComplete: scripts ends being a hardcoded to the html of the current systemMessageNotifier component. In other words, the #renderContentOn: of the systemMessageNotifier is called only once...and not on every ajax complete. And I understand why. 

What I don't know how to do is what I want hahhaa. I would try to avoid WebSockets for the moment. I thought maybe doing some jQuery polling ?

Thoughts?

Thanks in advance,  





--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Esteban A. Maringolo
2016-09-23 12:21 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> The problem, as usual, is all ajax requests (and my app is now almost everything with ajax).
> I need to hook on ajax calls so that I could also re-render the notifications components as
> there could have been new notifications since the last rendering.

> html
> document
> addLoadScript: (html jQuery document
>                      onAjaxComplete: (
>   (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith: [:r |
> self systemMessageNotifier renderContentOn: r.
> ]
> )).

> What I don't know how to do is what I want hahhaa.
>I would try to avoid WebSockets for the moment. I
> thought maybe doing some jQuery polling ?

I don't understand what you want to implement.

If you want to notify something, then such notification should come as
a response of the xhr and implement the ajaxComplete function handler
to parse the response searching for the notification you want to
display.

The alternative is to include a <script> in every ajax response to
update the notification on the client side.

I also don't see how you using websockets would benefit you unless you
hook the socket to an announcer.

Regards,

--
Esteban
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Mariano Martinez Peck


On Fri, Sep 23, 2016 at 3:23 PM, Esteban A. Maringolo <[hidden email]> wrote:
2016-09-23 12:21 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> The problem, as usual, is all ajax requests (and my app is now almost everything with ajax).
> I need to hook on ajax calls so that I could also re-render the notifications components as
> there could have been new notifications since the last rendering.

> html
> document
> addLoadScript: (html jQuery document
>                      onAjaxComplete: (
>   (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith: [:r |
> self systemMessageNotifier renderContentOn: r.
> ]
> )).

> What I don't know how to do is what I want hahhaa.
>I would try to avoid WebSockets for the moment. I
> thought maybe doing some jQuery polling ?

I don't understand what you want to implement.

Let's say... an admin user of the app, fires a broadcast message. This is a message object stored in the DB. All logged users, should render the active messages on the main header of the app. Something like "server is going to be restarted in 10 minutes". Or whatever. So what I wanted to do is that whenever the user "uses" the app, I would check if there are notifications and show them. With normal requests this is easy as the main messages header is re-rendered together with the whole page.
 
My problem is how to re-render that component (of the main header) on EACH/ALL ajax callbacks. Why? Because most of my app is now with ajax, none of the ajax calls will re-render the "header" nor the component that renders the alerts. So.. no alert is shown until I do a normal request. 

So...what I don't know how to do is a generic way in which I can re-render the messages component on each ajax callback. 
 

If you want to notify something, then such notification should come as
a response of the xhr and implement the ajaxComplete function handler
to parse the response searching for the notification you want to
display.

The notifications comes from a query in the DB. There could be notifications still valid and the user just entered to the app for example. 
 

The alternative is to include a <script> in every ajax response to
update the notification on the client side.


And how could I do this in a generic way (not on every ajax response) ?

Thanks in advance!
 

I also don't see how you using websockets would benefit you unless you
hook the socket to an announcer.

Regards,

--
Esteban
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Esteban A. Maringolo
Try this:

html document addLoadScript: (
  html jQuery document onAjaxComplete: (
    (html jQuery id: self  systemMessageNotifier mainDivId)
       load
         global: false; "to avoid infinite recursion see [1]"
         html: [:r | self systemMessageNotifier renderContentOn: r ]
     )
).

[1] If $.ajax() or $.ajaxSetup() is called with the global option set
to false, the .ajaxComplete() method will not fire.
<http://api.jquery.com/ajaxComplete/>
Esteban A. Maringolo


2016-09-23 16:07 GMT-03:00 Mariano Martinez Peck <[hidden email]>:

>
>
> On Fri, Sep 23, 2016 at 3:23 PM, Esteban A. Maringolo <[hidden email]>
> wrote:
>>
>> 2016-09-23 12:21 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
>> > The problem, as usual, is all ajax requests (and my app is now almost
>> > everything with ajax).
>> > I need to hook on ajax calls so that I could also re-render the
>> > notifications components as
>> > there could have been new notifications since the last rendering.
>>
>> > html
>> > document
>> > addLoadScript: (html jQuery document
>> >                      onAjaxComplete: (
>> >   (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith:
>> > [:r |
>> > self systemMessageNotifier renderContentOn: r.
>> > ]
>> > )).
>>
>> > What I don't know how to do is what I want hahhaa.
>> >I would try to avoid WebSockets for the moment. I
>> > thought maybe doing some jQuery polling ?
>>
>> I don't understand what you want to implement.
>
>
> Let's say... an admin user of the app, fires a broadcast message. This is a
> message object stored in the DB. All logged users, should render the active
> messages on the main header of the app. Something like "server is going to
> be restarted in 10 minutes". Or whatever. So what I wanted to do is that
> whenever the user "uses" the app, I would check if there are notifications
> and show them. With normal requests this is easy as the main messages header
> is re-rendered together with the whole page.
>
> My problem is how to re-render that component (of the main header) on
> EACH/ALL ajax callbacks. Why? Because most of my app is now with ajax, none
> of the ajax calls will re-render the "header" nor the component that renders
> the alerts. So.. no alert is shown until I do a normal request.
>
> So...what I don't know how to do is a generic way in which I can re-render
> the messages component on each ajax callback.
>
>>
>>
>> If you want to notify something, then such notification should come as
>> a response of the xhr and implement the ajaxComplete function handler
>> to parse the response searching for the notification you want to
>> display.
>
>
> The notifications comes from a query in the DB. There could be notifications
> still valid and the user just entered to the app for example.
>
>>
>>
>> The alternative is to include a <script> in every ajax response to
>> update the notification on the client side.
>
>
>
> And how could I do this in a generic way (not on every ajax response) ?
>
> Thanks in advance!
>
>>
>>
>> I also don't see how you using websockets would benefit you unless you
>> hook the socket to an announcer.
>>
>> Regards,
>>
>> --
>> Esteban
>> _______________________________________________
>> seaside mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com
>
> _______________________________________________
> seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Mariano Martinez Peck
Well, with help from Esteban the (via chat), this ended up working as I expected:


html document addLoadScript: (
html jQuery document onAjaxComplete:(
html jQuery 
ajax
global: false; "to avoid inifite recursion"
script: [ :s | s <<  ( (html jQuery id: self classicMenuComponent systemMessageNotifier mainDivId) 
replaceWith: [:r | self classicMenuComponent systemMessageNotifier renderContentOn: r ]
) ]
)
)



Thanks for the help!

On Fri, Sep 23, 2016 at 5:30 PM, Esteban A. Maringolo <[hidden email]> wrote:
Try this:

html document addLoadScript: (
  html jQuery document onAjaxComplete: (
    (html jQuery id: self  systemMessageNotifier mainDivId)
       load
         global: false; "to avoid infinite recursion see [1]"
         html: [:r | self systemMessageNotifier renderContentOn: r ]
     )
).

[1] If $.ajax() or $.ajaxSetup() is called with the global option set
to false, the .ajaxComplete() method will not fire.
<http://api.jquery.com/ajaxComplete/>
Esteban A. Maringolo


2016-09-23 16:07 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
>
>
> On Fri, Sep 23, 2016 at 3:23 PM, Esteban A. Maringolo <[hidden email]>
> wrote:
>>
>> 2016-09-23 12:21 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
>> > The problem, as usual, is all ajax requests (and my app is now almost
>> > everything with ajax).
>> > I need to hook on ajax calls so that I could also re-render the
>> > notifications components as
>> > there could have been new notifications since the last rendering.
>>
>> > html
>> > document
>> > addLoadScript: (html jQuery document
>> >                      onAjaxComplete: (
>> >   (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith:
>> > [:r |
>> > self systemMessageNotifier renderContentOn: r.
>> > ]
>> > )).
>>
>> > What I don't know how to do is what I want hahhaa.
>> >I would try to avoid WebSockets for the moment. I
>> > thought maybe doing some jQuery polling ?
>>
>> I don't understand what you want to implement.
>
>
> Let's say... an admin user of the app, fires a broadcast message. This is a
> message object stored in the DB. All logged users, should render the active
> messages on the main header of the app. Something like "server is going to
> be restarted in 10 minutes". Or whatever. So what I wanted to do is that
> whenever the user "uses" the app, I would check if there are notifications
> and show them. With normal requests this is easy as the main messages header
> is re-rendered together with the whole page.
>
> My problem is how to re-render that component (of the main header) on
> EACH/ALL ajax callbacks. Why? Because most of my app is now with ajax, none
> of the ajax calls will re-render the "header" nor the component that renders
> the alerts. So.. no alert is shown until I do a normal request.
>
> So...what I don't know how to do is a generic way in which I can re-render
> the messages component on each ajax callback.
>
>>
>>
>> If you want to notify something, then such notification should come as
>> a response of the xhr and implement the ajaxComplete function handler
>> to parse the response searching for the notification you want to
>> display.
>
>
> The notifications comes from a query in the DB. There could be notifications
> still valid and the user just entered to the app for example.
>
>>
>>
>> The alternative is to include a <script> in every ajax response to
>> update the notification on the client side.
>
>
>
> And how could I do this in a generic way (not on every ajax response) ?
>
> Thanks in advance!
>
>>
>>
>> I also don't see how you using websockets would benefit you unless you
>> hook the socket to an announcer.
>>
>> Regards,
>>
>> --
>> Esteban
>> _______________________________________________
>> seaside mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com
>
> _______________________________________________
> seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Esteban A. Maringolo
I would have done it this way:

html document addLoadScript: (
html jQuery document onAjaxComplete:(
(html jQuery id: self classicMenuComponent systemMessageNotifier mainDivId)
ajax
global: false; "to avoid inifite recursion"
html: [:r | self classicMenuComponent systemMessageNotifier renderContentOn: r ]
)
)

This MUST work. Otherwise I don't know... :D

Beware you're doing html jQuery ajax every time you evaluate the onAjaxComplete: function, and that is discouraged, because you're not working in an instance of jQuery, but instead on the class.



Esteban A. Maringolo

2016-09-23 17:58 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
Well, with help from Esteban the (via chat), this ended up working as I expected:


html document addLoadScript: (
html jQuery document onAjaxComplete:(
html jQuery 
ajax
global: false; "to avoid inifite recursion"
script: [ :s | s <<  ( (html jQuery id: self classicMenuComponent systemMessageNotifier mainDivId) 
replaceWith: [:r | self classicMenuComponent systemMessageNotifier renderContentOn: r ]
) ]
)
)



Thanks for the help!

On Fri, Sep 23, 2016 at 5:30 PM, Esteban A. Maringolo <[hidden email]> wrote:
Try this:

html document addLoadScript: (
  html jQuery document onAjaxComplete: (
    (html jQuery id: self  systemMessageNotifier mainDivId)
       load
         global: false; "to avoid infinite recursion see [1]"
         html: [:r | self systemMessageNotifier renderContentOn: r ]
     )
).

[1] If $.ajax() or $.ajaxSetup() is called with the global option set
to false, the .ajaxComplete() method will not fire.
<http://api.jquery.com/ajaxComplete/>
Esteban A. Maringolo


2016-09-23 16:07 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
>
>
> On Fri, Sep 23, 2016 at 3:23 PM, Esteban A. Maringolo <[hidden email]>
> wrote:
>>
>> 2016-09-23 12:21 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
>> > The problem, as usual, is all ajax requests (and my app is now almost
>> > everything with ajax).
>> > I need to hook on ajax calls so that I could also re-render the
>> > notifications components as
>> > there could have been new notifications since the last rendering.
>>
>> > html
>> > document
>> > addLoadScript: (html jQuery document
>> >                      onAjaxComplete: (
>> >   (html jQuery id: self  systemMessageNotifier mainDivId)  replaceWith:
>> > [:r |
>> > self systemMessageNotifier renderContentOn: r.
>> > ]
>> > )).
>>
>> > What I don't know how to do is what I want hahhaa.
>> >I would try to avoid WebSockets for the moment. I
>> > thought maybe doing some jQuery polling ?
>>
>> I don't understand what you want to implement.
>
>
> Let's say... an admin user of the app, fires a broadcast message. This is a
> message object stored in the DB. All logged users, should render the active
> messages on the main header of the app. Something like "server is going to
> be restarted in 10 minutes". Or whatever. So what I wanted to do is that
> whenever the user "uses" the app, I would check if there are notifications
> and show them. With normal requests this is easy as the main messages header
> is re-rendered together with the whole page.
>
> My problem is how to re-render that component (of the main header) on
> EACH/ALL ajax callbacks. Why? Because most of my app is now with ajax, none
> of the ajax calls will re-render the "header" nor the component that renders
> the alerts. So.. no alert is shown until I do a normal request.
>
> So...what I don't know how to do is a generic way in which I can re-render
> the messages component on each ajax callback.
>
>>
>>
>> If you want to notify something, then such notification should come as
>> a response of the xhr and implement the ajaxComplete function handler
>> to parse the response searching for the notification you want to
>> display.
>
>
> The notifications comes from a query in the DB. There could be notifications
> still valid and the user just entered to the app for example.
>
>>
>>
>> The alternative is to include a <script> in every ajax response to
>> update the notification on the client side.
>
>
>
> And how could I do this in a generic way (not on every ajax response) ?
>
> Thanks in advance!
>
>>
>>
>> I also don't see how you using websockets would benefit you unless you
>> hook the socket to an announcer.
>>
>> Regards,
>>
>> --
>> Esteban
>> _______________________________________________
>> seaside mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com
>
> _______________________________________________
> seaside mailing list
> [hidden email]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
>
_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



--

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Esteban A. Maringolo
2016-09-23 18:18 GMT-03:00 Esteban A. Maringolo <[hidden email]>:

> Beware you're doing html jQuery ajax every time you evaluate the onAjaxComplete: function, and that is discouraged, because you're not working in an instance of jQuery, but instead on the class.

Nevermind this, I got confused, it is the ajaxSetup that is discouraged. I've been using `html jQuery ajax` all over the place.

It's the #script:/replaceWith: that confused me, I'm used to do what you do inside #script: in an onSuccess:/onError: handler and use html: instead of #replaceWith:

Regards!

Esteban A. Maringolo


_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Execute jQuery script upon #onAjaxComplete?

Esteban A. Maringolo
This works:
html document addLoadScript: (
html jQuery document onAjaxComplete:(
(html jQuery id: self classicMenuComponent systemMessageNotifier mainDivId)
load
global: false; "to avoid inifite recursion"
html: [:r | self classicMenuComponent systemMessageNotifier renderContentOn: r ]
)
)


Given you modify the supported options to include the "global" option.

JQLoad>>#isSupportedOption: aString
^ #('url' 'data' 'complete' 'global' "(" 'dataType' ")") includes: aString


Given load() is a shorthand function in jQuery, there is no reason to not include such option in the supported ones.

Regards,


Esteban A. Maringolo

2016-09-23 18:28 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
2016-09-23 18:18 GMT-03:00 Esteban A. Maringolo <[hidden email]>:

> Beware you're doing html jQuery ajax every time you evaluate the onAjaxComplete: function, and that is discouraged, because you're not working in an instance of jQuery, but instead on the class.

Nevermind this, I got confused, it is the ajaxSetup that is discouraged. I've been using `html jQuery ajax` all over the place.

It's the #script:/replaceWith: that confused me, I'm used to do what you do inside #script: in an onSuccess:/onError: handler and use html: instead of #replaceWith:

Regards!

Esteban A. Maringolo



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside