0

i have a condition in my function . i want to set a value of a variable true or false on the basis of another variable whether it is empty of not in knockout js?

 self.editData = function (data) { 
     self.id(data.id());
     self.nscto(data.nscto());
     if (nscto != null && "".equals(nscto)){
         self.view(true)
     }
 }

here i write if condition as we use in java language i want to use this scenarion in knockout how can i do it ?

Milimetric
  • 13,411
  • 4
  • 44
  • 56
user2142786
  • 1,484
  • 9
  • 41
  • 74

3 Answers3

1

You have used two different variables (nsct and nsc) and an operator that doesn't exist (=!). The last part of the condition would be interpreted as an assignment: nsc = (!"").

Also, the logic is wrong, there is no value that is null and an empty string at the same time, so the condition would always be true. You would use the && operator instead:

if (nsct != null && nsct != "") {
  self.view(true);
}

If you want to set it to false if the condition isn't true, then you would use an else also:

if (nsct != null && nsct != "") {
  self.view(true);
} else {
  self.view(false);
}

You can also do that by using the value of the condition:

self.view(nsct != null && nsct != "");
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • i want to use it in knockout js is it the valid syntax for it – user2142786 Aug 28 '14 at 08:30
  • @user2142786: Yes. Knockout is just a Javascript library, it doesn't have any syntax of its own. – Guffa Aug 28 '14 at 08:36
  • is `nsct`/`nscto` defined? it isn't as far as your code shows. I think you intended to check against `self.nscto`. In which case, this answer will not work correctly because the observable is not unwrapped (I guess this is also what you mean by Knockout syntax). Since I think Guffa's last code-block is the nicest, I will demonstrate it with that block: `self.view(self.nscto() != null && self.nscto() != "");`. This can be made even simpler if you also want to check against `undefined` (which I think you do): `self.view(self.nscto())`. One could even argue you would not need `self.view` at all. – Hans Roerdinkholder Aug 28 '14 at 08:43
  • 1
    Additionally, I feel @Guffa should've used strict comparison operators, because you explicity check against both a null-value and an empty string value. If strict equality is not needed, that argues for my previous point of using `self.view(self.nscto())`. Agreed? – Hans Roerdinkholder Aug 28 '14 at 08:46
1

There are many ways to do what you want:

if ( null != self.nscto() && "" === self.nscto() )
{
    self.view(true)
}    

or

if ( null != self.nscto() && self.nscto().length === 0 )
{
    self.view(true)
}

or simpler

self.view( null != self.nscto() && "" === self.nscto() )
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • http://stackoverflow.com/questions/25637663/converting-circular-structure-to-json-in-knockout-js can you answer this ? – user2142786 Sep 03 '14 at 08:33
0

The way you have it written you would need

self.editData = function (data) { 
   self.id(data.id);
   self.nscto(data.nscto);
   if (self.nscto() != null && self.nscto() == ""){
        self.view(true);
   }
}
garyh
  • 2,782
  • 1
  • 26
  • 28