Webtags (preserving history): Difference between revisions

m
Line 698: Line 698:
=== JavaScript example ===
=== JavaScript example ===


I have a script that works with the standard log editor in the admin interface to calculate (and recalculate after any edit) the derived fields from the source fields.  To make these calculations work for anyone, I need to find out what units the Cumulus MX user is using. Here is the code:
I have a script that works with the standard log editor in the admin interface to calculate (and recalculate after any edit) the derived fields from the source fields.  To make these calculations work for anyone, I need to find out what units the Cumulus MX user is using. Here is the code (with the api call written using jQuery):
<pre>/*    Some new variables connected with new api call (MX 3.7.0)  */
<pre>/*    Some new variables connected with new api call (MX 3.7.0)  */
var tempLetter;  // C or F
var tempLetter;  // C or F
Line 712: Line 712:
console.log("new api", tempLetter, rainUnit,windUnit);
console.log("new api", tempLetter, rainUnit,windUnit);
}</pre>
}</pre>
A little bit of explanation might help:
*JavaScript variables generally need to be declared first, I have used 3 separate line each starting with '''var''', but you can list several variables on one line using a comma to separate them
*The jQuery get request takes the api URL as first parameter and the function to deal with the returned result as third parameter
*The middle parameter is irrelevant in this context, but if the qet was returning multiple results, "limit=1" would stop after first result
*The function takes as its sole parameter an Object, this is what is returned by the api, the selected name can be any variable name
*The object returned has 3 parameters each in '''attribute=value''' format
*We find the value in any parameter by using the '''object.attribute''' syntax
*The console.log command simply outputs all the items in the list within the brackets into the browser console that in many browsers is displayed by selecting '''F12''' on the keyboard


== "POST" approach ==
== "POST" approach ==
5,838

edits