We help IT Professionals succeed at work.
Get Started

Accessing observable variable before and after setting a value is not working

gopikrish asked
Last Modified: 2016-09-25
I have 2 variables declared as mtn and atn whose values get populated based on the API ajax response.

Now, I am assigning mtn and atn variables with a value returned from API response and in my case since mtn is coming as null whereas atn is having a 10 digit number. So I am assigning atn1, atn2, atn3 with substring of atn whereas mtn1, mtn2 and mtn3 is untouched as you can see from below code snippet.

var ViewModel = {
mtn: null,
atn: null,
mtn1: ko.observable(null),
mtn2: ko.observable(null),
mtn3: ko.observable(null),
atn1: ko.observable(null),
atn2: ko.observable(null),
atn3: ko.observable(null)
// Ajax API call happens and assigns ViewModel.mtn and ViewModel.atn
if (ViewModel.mtn != null && ViewModel.mtn.length == 10) { //This condition is not true because ViewModel.mtn is coming as null in case and so mtn1, mtn2 and mtn3 observables are untouched
ViewModel.mtn1 = ViewModel.mtn.substring(0, 3);
ViewModel.mtn2 = ViewModel.mtn.substring(3, 6);
ViewModel.mtn3 = ViewModel.mtn.substring(6, 10);
if (ViewModel.atn != null && ViewModel.atn.length == 10) {
ViewModel.atn1 = ViewModel.atn.substring(0, 3);
ViewModel.atn2 = ViewModel.atn.substring(3, 6);
ViewModel.atn3 = ViewModel.atn.substring(6, 10);

// Click event happens from View and Business logic UI validations take place below
processClick: function() {
     .var strmtn = ViewModel.mtn1 + ViewModel.mtn2 + ViewModel.mtn3; // This is not working and throwing error and only ViewModel.mtn1() is working
      var stratn = ViewModel.atn1 + ViewModel.atn2 + ViewModel.atn3; // This is working fine but ViewModel.atn1() is not working.

Open in new window

I believe the reason for the method of accessing atn1 as ViewModel.atn1 is working whereas method of accessing mtn1 as ViewModel.mtn1 is not working is because of re-assignment of observable for atn1 whereas mtn1 is not touched.

So how do I know which method of access to invoke an observable value since I don't know if my observables were touched or not ? :(
Watch Question
Senior Developer
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE