Link to home
Start Free TrialLog in
Avatar of gvkr
gvkr

asked on

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                                          
                                    }
                              },
                              
                              });
Avatar of Rob
Rob
Flag of Australia image

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

ASKER

if i store it, will that get updated, if i change the value of it?
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                                          
                                    }
                              };
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.
Avatar of gvkr

ASKER

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
                                          }
                                    },
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
                                          }
                                    },
Avatar of gvkr

ASKER

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?
Avatar of gvkr

ASKER

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
Avatar of gvkr

ASKER

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.
I'll work with your demo but i'm thinking along the lines of posting the original values at the bottom of the page
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gvkr

ASKER

I was thinking may be we can use subscribe?
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.
Avatar of gvkr

ASKER

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.
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?
Avatar of gvkr

ASKER

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.
Avatar of gvkr

ASKER

It is working, thank you for your help.
No problem :)