0

So with Knockout I can show text based on if a condition returns true or false like this:

data-bind="text: status==0 ? 'Sent' : 'Failed'"

However, I need to add another condition, so that if status==1 then it returns Pending. I guess this probably just a general JS question vs Knockout.

Anyway, is it possible to do something like that? Thanks!

1 Answer 1

2

Sounds like you really want to use Knockout's Computed Observables. This will allow you to return different computed values based on your status.

function ViewModel() {
  this.status = ko.observable();
  this.statusText = ko.computed(function() {
    if (this.status() == 0) {
      return 'sent'
    } else {
      return 'failed';
  }, this);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's what it was!! I could've sworn there was a way to use a function in a binding like that, I just couldn't find it again. Thanks! :)
No prob. Just updated the answer because I submitted before I finished my code example.
Also the logic is moved to the VM, much cleaner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.