Hi,
I created a small app that fetch data from a weather API.
The process is started when i clic on a button but i can’t figure oit how to do it simply when the page loads.
my main.html file code is :
<head>
<title>wthr</title>
</head>
<body>
<h1>Weather app</h1>
{{> hello}}
</body>
<template name="hello">
<button class="btn">Click Me</button>
<p>La prévision météo est {{counter.currently.icon}} avec un un indice ozone de {{counter.currently.ozone}}</p>
</template>
My main.js on the client side is :
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
Template.hello.onCreated(function helloOnCreated() {
// counter starts at 0
this.counter = new ReactiveVar(0);
});
Template.hello.helpers({
counter() {
return Template.instance().counter.get();
},
});
Template.hello.events({
'click .btn': function (evt,instance) {
var latitude = 43.6109200;
var longitude = 3.8772300;
Meteor.call('sendData', latitude, longitude, function(error,response){
if(error){
console.log('ERROR :', error);
}else{
console.log('Response :', response);
instance.counter.set(response);
}
})
}
});
and my main.js code on the client side is :
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
// code to run on server at startup
Meteor.methods({
'sendData': function(){
var weatherman = require( "weatherman.io" );
var citiesArray = [
{
//name: "Montpellier",
latitude: 43.6109200,
longitude: 3.8772300
},
{
//name: "Nîmes",
latitude: 43.8333300,
longitude: 4.3500000
}];
console.log(citiesArray[1].latitude);
var options = {
units: "si",
lang: "fr",
exclude: ["minutely", "alerts"],
extend: "hourly"
};
var alRoker = new weatherman( "db0ee89a0f17a93af5b7556eaf28db18", options );
var forecastOptions = {
latitude: 43.6109200,
longitude: 3.8772300
};
Async = Meteor.wrapAsync(alRoker.doForecast, alRoker);
const weatherReport = Async(forecastOptions);
//console.log(weatherReport);
return weatherReport;
}
})
});
how can i start the whole process directly when the html page is loaded instaed of clicking a button ?
thanks