Web Analytics Made Easy -
StatCounter Accessing observable variable before and after setting a value is not working - CodingForum

Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

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

    I have 2 variables declared mtn and atn whose values get populated based on API ajax response.

    Now, I am assigning mtn and atn variables with a value returned from API response and since mtn is coming null or blank whereas atn is having proper value, so I am assigning atn1, atn2, atn3 with substring of atn whereas mtn1, mtn2 and mtn3 is untouched.

    Code:
    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) {
    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);
    }
    ko.applyBindings(ViewModel);
    
    // 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.
    I believe the reason why ViewModel.atn is working whereas ViewModel.mtn 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 API returns values for it or not ?

  • #2
    Just a fast look and you don't close the last function and there is a dot in front of first variable.
    Code:
    // Click event happens from View and Business logic UI validations take place below
    processClick: function() {
         [U][COLOR="#FF0000"].[/COLOR][/U]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.
    [U][COLOR="#FF0000"]}[/COLOR][/U]
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment

    Working...
    X