I'm using a framework (meteor, but doesn't matter for the question I guess) which provides separate functions..
My problem is I realize I am using more and more global variables in order to make them accessible between these functions. For example the map object:
Meteor.startup(function () {
map = L.map('map_canvas').locate({setView: true, maxZoom: 21});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
});
I need to access map everywhere to manipulate my maps(with the use of Leaflet) For example:
Template.messages.events({
'click .delete-message': function (e,t) {
e.stopPropagation();
removeItem(e.target.id);
},
'click .message': function (e,t) {
console.log('event clicked');
map.setView([e.target.getAttribute('data-lat'),e.target.getAttribute('data-lng')], 16);
}
});
I have the same problem when I want to create a marker object that I want to use in different places...
Is it that Meteor is built this way, or are there more proper/clean JS alternatives rather than making stuff global?
edit Thanks for your answers, could you please add an example of code using for example one of the patterns you mention but based on my code? This way I could understand it much better.