0
var viewModel = {
...
defaultTextProject: ko.observable(),
...
}    

function assignProject(params) {
            var projectNo = params.itemData["projectNo"];
            viewModel.selectedProjectTile(projectNo);
            var projectName = params.itemData["projectName"];
            aktivEmployee.aktivProjectId = projectNo;
            aktivEmployee.projectName = projectName;
            viewModel.aktivProjectId(projectNo);
            viewModel.defaultTextProject(projectNo + " " + projectName);

When I hit the last line in the above aktiveEmployee.aktivProjectId and aktivEmployee.projectName become undefined. However, vars projectNo and projectName retain their assigned values. All the other properties for the aktivEmployee object remain as they should.

Also, after the last line viewModel.defaultTextProject() displays what I want which shows the vars mentioned above still keeping their names but my aktivEmployee objects .aktivProjectId and .projectName are losing these values.

My first thought was the viewModel changing was losing them but it doesn't happen until the 3rd viewModel update so there's something here I'm not understanding with Javascript objects.

Does anyone have any idea what's going on here?

Thanks in advance!

rory
  • 1,490
  • 3
  • 22
  • 50
  • Then projectName (the local variable) evaluates to undefined after the assignment and an undefined value is set for aktiveEmployee.projectName - *or* the problem is not exactly as described/believed. (It may be the case that the debugger/watch list is showing old values or it is being red incorrectly, or that aktiveEmployee is a different object or the properties are set elsewhere...) – user2864740 Sep 12 '14 at 08:26
  • @user2864740 the local projectName does not evaluate to undefined while the function is running. That's why I'm so confused – rory Sep 12 '14 at 08:30
  • I've added some additional possibilities. If an assignment sets a property of aktivEmployee, then it will stay set *for that object* unless it is reassigned/deleted. The assignment happens by-value (so the local variable or value-of is irrelevant after the assignment). – user2864740 Sep 12 '14 at 08:31
  • @user2864740 thanks, where I thought the aktivEmployee object was losing two values it must have been reassigned somewhere in the backend knockout.js which leads me to believe a page refresh (that I haven't called!) is happening somewhere – rory Sep 12 '14 at 08:42
  • @user2864740 solved. viewModel.aktivProjectId(projectNo) was resetting the values for the params and recalling the function. Couldn't have got it without realising that the object was being re-assigned and not jsut losing values! – rory Sep 12 '14 at 08:54
  • Yay! I'm glad the problem was solved. It seems odd that an observable would modified a non-observable though. – user2864740 Sep 12 '14 at 08:55

0 Answers0