Search code examples
jqueryjsonknockout.jsjquery-templates

jQuery tmpl {{if}} condition with false not working


I am using this in part of my template:

{{if !IsDefault}}
    <a href="#" onclick="makeDefault('${Id}');return false;">Make Default</a>
{{/if}}

Except when IsDefault is false it is not working. I even tried:

{{if IsDefault === false}}` and `{{if IsDefault == false}}

I have verified that the value actually is false in my json object that gets passed to the template.

I also tried this which works but is pretty ugly and I would prefer for it to just work how it should:

{{if IsDefault}}
{{else}}
    <a href="#" onclick="makeDefault('${Id}');return false;">Make Default</a>
{{/if}}

Also, if I change it to check for a true condition (doesn't make sense but just tried it for debugging purposes)...

{{if IsDefault}}

...it works fine. Looks like it just has problems with the false values. I am getting the json via an jQuery ajax call and using knockout mapping with jquery tmpl. I get no js errors.

Any ideas on why testing for false or !false is not working?


Solution

  • Try using {{if !(IsDefault())}}

    If you are using an observable that is not in an expression, then jQuery templates sees that it is a function and calls it as a function to get the observable's value. If it is in an expression, then this doesn't happen and you need to call your observable as a function (with no args) to get its value.