i having a problem with a global method called returnDayNumber. If i call the function within my java script it works fine. but when i made a button to call the function it said: “self.returnDayNumber is not a function” (thought var self=this might solve issue).
here is my code:
/*
* Global data
* used to centralize user data to be used in graphs
*/
var userModel = new userHealthModel();
userModel.createUsers();
userModel.addUser();
var newChart = new NewHighCharts();
returnDayNumber = function(month, day)
{
if(month==1)
{
return day;
}
else if(month==2)
{
return(31+day);
}
else if(month==3)
{
return(60+day);
}
else if(month==4)
{
return (91+day);
}
else if(month==5)
{
return(121+day);
}
else if(month==6)
{
return(152+day);
}
else if(month==7)
{
return(182+day);
}
else if(month==8)
{
return(213+day);
}
else if(month==9)
{
return(244+day);
}
else if(month==10)
{
return(274+day);
}
else if(month==11)
{
return(305+day);
}
else if(month==12)
{
return(335+day);
}
}
saveThis = function()
{
var self = this;
alert("your about to save!")
newChart.destroyChart();
newChart= new NewHighCharts();
var numberOfUsers = userModel.users().length;
var users = userModel.users();
var weights = userModel.getWeights();
var dates = userModel.getDates();
var dataArray = [];
var xAndY = []
var singleDate;
var name;
for(var i=0;i<numberOfUsers;i++)
{
name = users[i].name;
xAndY = [];
for(var x=0;x<dates.length-1;x++)
{
//extract month
var month = ([dates[i][x]].toString()).substring(0,2);
//extract day
day = parseInt(([dates[i][x]].toString()).substring(3,5));
//add the day number and weight of users to an array of x and y coordinates
xAndY.push([self.returnDayNumber(month,day),weights[i][x]])
}
newChart.addNewSeries(name,xAndY);
}
///!!!!This works!!!///
this.saveThis()//!!!
}
ko.applyBindings(userModel);
i made a KO user model but i dont think its relevant. and here is my HTML, note its KO syntax but it works.
<button data-bind="click: saveThis">Save</button>
im probably making a simple mistake or something is out of scope. Im a java script beginner so sorry for the simple question. Many thanks to any helpful programmers!
Move
var self = this;outside of yoursaveThisfunction not inside, that wayselfis in the right scope.