We help IT Professionals succeed at work.

knockout calculating value based on flag

gvkr
gvkr asked
on
what i have to do is for the first time i have to do some logic and and change the flag, then after that i have to apply another logic, i did like this, in read, first value for isValid will be true, so it goes to else block where we make it as false from next time it should go inside if block

vm.value = ko.computed(
                  {
                   read : function() {
                        if($("#isValid").val()!=="true"){
                                             //do some logic
                              }else{

                                 $("#isValid").val("false");
                                                // do some logic                                          
                                    }
                              },
                              
                              });
Comment
Watch Question

RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
Can you not just store the flag in your viewModel, even if it is based on a value in your markup?

Author

Commented:
if i store it, will that get updated, if i change the value of it?
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
that's up to you, not every variable of your view model needs to be observable.  In fact a flag most likely should not as you may not want it triggering any knockout events.

e.g. In the case above you could try having it as not being a computed function but rather just a normal function:


vm.value = function() {
                        if($("#isValid").val()!=="true"){
                                             //do some logic
                              }else{

                                 $("#isValid").val("false");
                                                // do some logic                                          
                                    }
                              };
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
It only needs to be "computed" if something in your view is using it to display content and needs to be called and updated.

e.g. You're storing first and last name but your displaying the full name in your view.  That needs to be computed because when you change the first or last name, you'd want the function to be called.

Author

Commented:
This is what i tried but i am getting the error
if(vm.isValid!=="true"){
                                    // do some logic
                                    }else{
                                          
                                          vm.isValid("false");
                                          do some logic
                                          }
                                    },
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
vm.isValid should just be defined like:

vm.isValid = true; // or false but without the quotes.

Then your code above should read:

if(vm.isValid){
                                    // do some logic
                                    }else{
                                         
                                          vm.isValid = false;
                                          do some logic
                                          }
                                    },

Author

Commented:
vm.isValid is coming from a form;
vm.valid is the value of form field, so it is being appended with "".So, then also in else bock do i not use quotes?

Author

Commented:
Actually what i have to do is in this example
http://jsbin.com/pakaje/2/edit
When we run this script it loads with 45, 0 and 88, 0. If i change the value to 48 the values will be 48, -3. These value will be submitted, but when i have the error from back end i want to still display 48, -3. Isuuse 48 is staying but differnece is becoming zero, i had thought of using the flag it did not work

Author

Commented:
Actually what i have to do is in this example
http://jsbin.com/pakaje/2/edit
When we run this script it loads with 45, 0 and 88, 0. If i change the value to 48 the values will be 48, -3. These value will be submitted, but when i have the error from back end i want to still display 48, -3. Isuuse 48 is staying but differnece is becoming zero, i had thought of using the flag it did not work, so what i did was use the flag not to calcluate the difference between old and new value, it does load with proper values in when the page loads but if the value changes it is not getting calculated.
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
I'll work with your demo but i'm thinking along the lines of posting the original values at the bottom of the page
Owner (Aidellio)
Most Valuable Expert 2015
Commented:
http://jsbin.com/zunudikuwe/1/edit

I've added the value attribute (and the ID for reference) to each of your input variables.  

I've also added a variable (orig) at the end of your markup, in script tags.  Your server script must add this.

Author

Commented:
I was thinking may be we can use subscribe?
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
subscribe still needs a reference point.  I'm also of the opinion that you should over use knockout for this.  subscribe is ok when a value changes in your view / model.

Author

Commented:
May be i have misguided on the page load both the values will be zero,
if the user enters 100, value and difference should become 100, if there is an error from server it should still hold the value of 100 as original value and change as 100, if  change the value to 200, it difference should be 200 and it similar for difference also. I.e when the page loads difference and value should 0, but if the difference changes to 100, value should change to 100 and if i get the error from server it should hold both values as 100 and if i change the difference to 200, value should automatically change to 200.
On page load difference is always zero, except in error conditions.
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
That's how I understand it too.  Did you see the <script> at the bottom of the markup?  You set this on page load AND when there is an error from the server.
Are you using AJAX to submit the form or posting the page?

Author

Commented:
Yes, i think i have figured what i have missing i.e. i am making the origianl value as 48 insted of 45, will change that an try, will keep you posted.

Author

Commented:
It is working, thank you for your help.
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
No problem :)