How to add JSON weather to element

thewedge

New Member
I have a JSON open weather issue that I need help with. I have read through a lot of documentation and it all seems different and don't know what to use to make this work. I have an element that is read only and I need to put into that field a JSON data. I have this running on another system so I know it works. I found Fabrik and it offers some things that my other system doesn't, so I am seeing if I can create what I am building on Fabrik.

Here is my code. This is under Javascript of the element
function weatherforecast() {
var q = form_2.formElements.get('property_logs___weather').getValue();
var t = jQuery.getJSON('link_to_api',
function(data) {
var obj = data;
if(q ===''){
q = "Forecast: "+obj.main.temp+"?C "+obj.weather[0].description;
}
});

form_2.formElements.get('property_logs___weather').update(q);
}
I had to take out the link as this forum won't let me add the api, but I do know it works.
 
So I took a look at this again and followed a wiki of you js file. I create a form_2.js file and I put it in components > com_fabrik > js . I then put the function in the javascript panel for that element. I then used onload as my trigger. Nothing happened. I then decided to look at the source code and couldn't find the actual function in the head or any other place on the source code page. Either I am doing something totally wrong, or this doesn't work.
 
Try this.

In your element js for the 'onload', do ...

weatherforecast(this);

... then in your form_2.js ...

Code:
function weatherforecast(el) {
   if (el.getValue() === '') {
      jQuery.getJSON(
         'link_to_api',
         function(data) {
            el.update("Forecast: "+data.main.temp+"?C "+data.weather[0].description);
         }
      );
   };
}

-- hugh
 
Nope still doesn't show in form field. However I do now see that the form_2.js is in the source and this:

form_2.dispatchEvent('field', 'property_logs___weather', 'load', 'weatherforecast(this);');

is in the source pointing to the correct field.

I think I need to do a simple test to see if anything gets put into the field? Suggestions?
 
Just looked at your form_2.js, and I think you are working from an uncorrected post. I edited my post within a few seconds, to correct a few typos.

The corrected code is:

Code:
function weatherforecast(el) {
   if (el.getValue() === '') {
      jQuery.getJSON(
         'link_to_api',
         function(data) {
            el.update("Forecast: "+data.main.temp+"?C "+data.weather[0].description);
         }
      );
   };
}

-- hugh
 
Might not work if you hide it completely with ACL's though, rather than just using "Hidden" on a simple field element.

-- hugh
 
Back
Top