What it does
I have a simple webpart that pulls in train information to display how many minutes are left and displays the data in the UI. There is a refresh link to refresh the data in the UI. When the results are acquired, it appends the data to either the red line or yellow/green line as shown below.
UI Example
REFRESH LINK
RED LINE TRAINS:
Silver Spring - Minutes: 1
Twinbrook - Minutes: 1
Glenmont - Minutes: 6
Twinbrook - Minutes: 9
Silver Spring - Minutes: 10
Grosvenor-Strathmore - Minutes: 11
YELLOW/GREEN LINE TRAINS:
Greenbelt - Minutes: ARR
Greenbelt - Minutes: 2
Franconia-Springfield - Minutes: 3
Branch Ave - Minutes: 5
Mt Vernon Sq 7th St-Convention Center - Minutes: 6
Branch Ave - Minutes: 10
The problem
This code works fine in the Classic experience webpart. However, in the Modern experience, I've observed that the webpart is being refreshed. This changes the state of the HTML to that of when the app was first added, ignoring the properties in my drop downs. This means an html container will no longer be present to have the data attached to.
Aside from the architectural issues I've been given, is there a way that I can stop the webpart from refreshing? The ajax call should run independently of the webpart.
Code sample
function loadMetro() {
$(".trainEntry").remove();
var redLine = $("#wmatared" + token);
var yellowLine = $("#wmatayel" + token);
var wmataAPI = "https://api.wmata.com/StationPrediction.svc/json/GetPrediction/B01,F01";
$.ajax({
data: "api_key=67te5y756nwun3x584v6hy3k",
url: wmataAPI,
success: function(parsed_json) {
console.log("Results are in");
var k = parsed_json["Trains"].length;
$.each(parsed_json["Trains"], function(k, v) {
var line = v["Line"];
var car = v["Car"];
var dest = v["DestinationName"];
var mins = v["Min"];
if(line == 'RD') {
redLine.append("<li class='trainEntry' data-id=" + car + ">" + dest + " - Minutes: " + mins + "</li>");
}
if(line == 'YL' || line == 'GR') {
yellowLine.append("<li class='trainEntry' data-id=" + car + ">" + dest + " - Minutes: " + mins + "</li>");
}
});
},
error: function() {
$("#wmata").append("Error receiving data from WMATA web service.");
}
});
}
Here is some detail outside of the problem
This code is loaded in client sites via Remote Provisioning. The webpart is a React SPFX webpart that allows the user to select an application. Each application is really a javascript file. This emulates what a content editor/script editor webpart could do, but with better functionality around it.
Here is a link to the gist of 3 pages https://gist.github.com/anonymous/44b70cdcba0d119282bdba40937b0b6b