I have a dropdown menu which allows users to select a certain month. An observable array changes the layers of my leaflet map according to user selection (works perfectly).
Now I need the selection value myMonth
(zero based month number) for another function which populates a popup window with content. I just don't find a working solution for using the variable myMonth
outside the viewModel
function... Any help highly appreciated!
Here's my code which results in: popupcontent = "Selected month: undefined"
var myMonth; //global variable myMonth
//oberservable array
function viewModel() {
this.choices = ko.observableArray([
"January","February","March","April","May","June",
"July","August","September","October","November","December"]);
this.selectedChoice = ko.observable();
this.selectedChoice.subscribe(function(newValue) {
myMonth = this.choices.indexOf(this.selectedChoice());
myLayerGroup.clearLayers();
myLayerGroup.addLayer(myLayers[myMonth]);
console.log(myMonth); //works!
return myMonth; // no effect?!
},this);
};
ko.applyBindings(new viewModel());
// popUp window content
function onEachFeature(feature, layer) {
if (feature.properties) {
var popupContent = "Selected month: "+ myMonth;
layer.bindPopup(popupContent);
}
};