knockout calculating value based on flag

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                                          
                                    }
                              },
                              
                              });
gvkrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Can you not just store the flag in your viewModel, even if it is based on a value in your markup?
gvkrAuthor Commented:
if i store it, will that get updated, if i change the value of it?
RobOwner (Aidellio)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                                          
                                    }
                              };
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

RobOwner (Aidellio)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.
gvkrAuthor 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)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
                                          }
                                    },
gvkrAuthor 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?
gvkrAuthor 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
gvkrAuthor 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)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
RobOwner (Aidellio)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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gvkrAuthor Commented:
I was thinking may be we can use subscribe?
RobOwner (Aidellio)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.
gvkrAuthor 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)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?
gvkrAuthor 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.
gvkrAuthor Commented:
It is working, thank you for your help.
RobOwner (Aidellio)Commented:
No problem :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.