Issue with 'rb.newui.util.customEvents.rbs_pageRender

Posted by ithrees on 28-Sep-2016 04:30

Hi All,

How exactly the 'rb.newui.util.customEvents.rbs_pageRender' event can be handled to call something on a specific page load in rollbase 4.2? 

<script>
  rb.newui.util.addEventListener(rb.newui.util.customEvents.rbs_pageRender,function(){
    console.log('page is rendered');
    initialize(); // a function in the page script
  });
</script>

I tried by adding the above script in the page itself, it is not working when the page is refreshed and it is loading only in the second attempt of ajax loading. When I tried it in the header or custom side bar then I get script error.

Please advise,

Thanks in advance,
Ithrees

All Replies

Posted by Mani Kumar on 28-Sep-2016 04:44

Hi Ishak,

Can you please try something like this in sidebar and let me know if it works for you?

<script> 
if ( rbf_isNewUI() ) { //Event handling for onload/pageRender of New UI 
$(document).on(rb.newui.util.customEvents.rbs_pageRender, function() { sidebarScript(); }); //invoking event handler on container instead of node. 
} else { //defer this if none of users are on oldUI
$(document).ready(function() { 
sidebarScript(); 
}); 

function sidebarScript() { alert('Test'); } 
</script>


Regards,

 Mani.

Posted by ithrees on 28-Sep-2016 05:09

Hi Mani,

Thank you for your reply. I tried something similar and I am getting the same script error telling that the function is not defined.

And sidebar script is common to all the page and I want this to a specific page. How can I achieve that?

Thanks & Regard,

Ithrees

Posted by Mohammed Siraj on 28-Sep-2016 05:28

rb.newui.util.customEvents.rbs_pageRender is a custom event that will fire on both Ajax based page update and page refresh. Hence, it should be preferred over document.ready event.

Can you please share a sample app exhibiting the issue. Also, are there any console errors reported? It could be related to some page configuration issue wherein relevant script definitions are not loaded.

Posted by Mani Kumar on 28-Sep-2016 05:28

Tried the below script and is working fine for me, can you please try this by adding a component to a specific page on which you would like to :

<script>

   $(document).on(rb.newui.util.customEvents.rbs_pageRender,function(){

   alertScript(); });

   function alertScript() { alert('Test');

}

</script>

 

Regards,

Mani.

Posted by ithrees on 28-Sep-2016 06:44

Hi,

@Mani

This code fires the alert but in a strange way.

It fires the alert when you load the page as well as when you leave the page. Also if you keep opening the page again and again it keeps increasing the number times that the same alert pops up.

@Siraj

I created a new application to test this separately and I am still getting the issue. There is no error in the console as well.

My rollbase  version is 4.2.0.0

Thanks & Regards,
Ithrees

Posted by Mani Kumar on 28-Sep-2016 06:48

Can you please share the application xml so that we could try it locally?

- Mani.

Posted by ithrees on 28-Sep-2016 07:07

How can I share it with you?


Best Regard,
Ithrees

Posted by Mani Kumar on 28-Sep-2016 07:31

You can attach the app.xml to this thread by Switching to Rich Text Editor mode of comment and selecting Insert/edit Media icon and from drop down select the Computer(Upload)

- Mani.

Posted by ithrees on 28-Sep-2016 07:42

I thought those are only for images and videos.  :)

[View:/cfs-file/__key/communityserver-discussions-components-files/25/Test-Ajax-Load_5F00_v1.xml:200:50]

Thanks and Best Regards,
Ithrees

Posted by Mani Kumar on 29-Sep-2016 04:45

Hi Ishak,

Thank you for sharing the app.xml. I’m now able to figure out the issue and below are my recommendations:

 rb.newui.util.addEventListener is something, which will attach the Listener on page canvas element and as Siraj mentioned in his earlier comment, rb.newui.util.customEvents.rbs_pageRender fires on both update and refresh of pages and these listeners will be removed automatically on each page refresh. However, it is not in case of $(document).

 

$(document).on will attach handlers to document root node and will not get automatically removed when doing update. The reason behind why you were seeing incremental pop-ups was that, each time we try to load the same page or move to other page instead of refresh, a new handler gets added but the old one is not removed. Hence it is not suggested to use $(document).on in Ajax based pages.

 

To fix this, here is a simplified solution:

 

  1. When using in custom side bar:

 <script>

$(document).on(rb.newui.util.customEvents.rbs_pageRender, function() {

alertScript(); });

function alertScript() { alert('Hello from Custom Side Bar');

}

</script>

 

 2. On Ajax based pages:

 <script>

 rb.newui.util.addEventListener(rb.newui.util.customEvents.rbs_pageRender,function(){

   alertScriptPage(); });

   function alertScriptPage() { alert('Hello from page Script component');

}

</script>

 Also, if you would like to use in both pages and custom sidebar, please make sure that function names are distinctly named, else, you will end up having multiple pop-ups again.

 I’ve made the above changes in the application you’ve provided and re-attached it. Please try and let me know if it works at your end?

 

Regards,
Mani.

[View:/cfs-file/__key/communityserver-discussions-components-files/25/Test-Ajax-Load_5F00_v3.xml:320:240]

Posted by ithrees on 29-Sep-2016 05:32

Hi Mani,

Thank you for your reply and comments.

I tried the second one with my ajax based pages and I found it is working fine if it is the only script in the page.

Since I have hundreds of lines with many functions in the page script, I found 2 things,

1. It is only working at the first time if I separate this script to another script component. Otherwise it is not working at the first time of ajax load but from 2nd attempt onward it is working.

2. It is giving scripting errors if I call a function that is somewhere in the same page saying that is not defined. But when I put a timeout to call the same function after a second it is working as expected.

Thanks & Best Regards,

Ithrees

Posted by Mani Kumar on 03-Oct-2016 00:00

Hello,

This would need a much deeper insight to understand the issue. Could you please log a support ticket ?

Regards,

Mani.

This thread is closed