2

Background: I'm modifying a SharePoint list web part using JSLink. I'm also adding jQuery and jQuery-UI to make the list items display as the jQuery Accordion. It works well, except that I also need to implement the ajax automatic refresh on the web part to refresh the content every 60 seconds.

Problem: When the web part refreshes, the jquery code reverts - the items no longer show in accordion mode. I can open the browser console and type the jquery code manually, e.g., $(".selector").accordion(); and it works fine. This makes me think that I need to find a way to call the jquery code after each web part automatic refresh completes.

Question: So, is there a javascript event or way to find out when an automatic refresh triggers on my webpart so that I can call again my jquery accordion after? Is there something else I could be missing?

Thanks for your time!

jorluiseptor
  • 55
  • 2
  • 6

2 Answers2

1

The answer was to use this code:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(MyFunction); 

function MyFunction() { 
    //do something here; 
}

Credit to @Thriggle

References: https://msdn.microsoft.com/en-us/library/bb311028.aspx https://www.daniweb.com/programming/web-development/threads/247263/ajax-postback-after-endrequest

Nathan
  • 4,358
  • 2
  • 10
  • 26
jorluiseptor
  • 55
  • 2
  • 6
  • Feel free to mark this as the answer (now that you've got 15+ reputation) so people know your question is resolved. (And yes, [it's kosher to answer your own questions](http://stackoverflow.com/help/self-answer)!) – Thriggle Oct 08 '15 at 20:10
0

I believe you can insert your own code or function calls into the callback chain by overriding the _onFormSubmit method of the current instance of the Sys.WebForms.PageRequestManager object.

Sys.WebForms.PageRequestManager.getInstance()._onFormSubmit = function(i){

    Sys.WebForms.PageRequestManager.prototype._onFormSubmit.call(this,i);

    alert("Refreshing the data..."); // -- your code or function call here

};

When I ran the above code in the F12 console on a page with a list view that had a 25-second refresh, I started seeing the "Refreshing the data..." pop-up every 25-seconds, but I haven't tested it with anything more complicated than that.

Thriggle
  • 7,009
  • 2
  • 26
  • 37
  • Actually, your response let me to my answer. The code you gave me runs _right before_ the auto-refresh happens. I need it _right after_. So I looked on the web and found a very similar code: `Sys.WebForms.PageRequestManager.getInstance().add_endRequest(MyFunction); MyFunction() { //do something here; }` I want to give you credit for the answer. @Thriggle – jorluiseptor Oct 08 '15 at 19:18
  • I'm glad I helped a little! Good find with the [Sys.WebForms.PageRequestManager](https://msdn.microsoft.com/en-us/library/bb311028.aspx) MSDN class reference. – Thriggle Oct 08 '15 at 20:06