Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's hard to tell without seeing the rest of your code, but I think you have a context issue. You are defining your computed observable on your view model, but your code is kinda assuming it's defined on each object in the projects observable array. It's not, so your third line can't work. It's defined on the view model, which has no way or knowing or tracking which project in the <code>foreach</code> you're referencing.</p> <p>You have a couple of options. You could change <code>projectStatusStyle</code> to a function on your view model that takes the name as a parameter:</p> <pre><code>vm.projectStatusStyle = function (projStatusName) { var x = "left label label-large label-info"; var style = "left label label-large label-" switch (projStatusName()) { case 'Live': x = style + "success"; break; case 'Identified': x = style + "info"; break; case 'In conversation': x = style + "purple"; break; case 'Complete': x = style + "grey"; break; case 'Unsuccessful': x = style + "yellow"; break; } return x; }; </code></pre> <p>Then you can have a binding like </p> <pre><code>&lt;span data-bind="css: $root.projectStatusStyle(projectStatus().name)"/&gt; </code></pre> <p>You could use <code>$root</code> or <code>$parent</code>, depending on how your view model is defined. </p> <p>Honestly, I would probably just do this in your <code>foreach</code>:</p> <pre><code>&lt;span class="left label label-large" data-bind="css: {'label-info': projectStatus().name() === 'Identified', 'label-success': projectStatus().name() === 'Live', 'label-purple': projectStatus().name() === 'In conversation', 'label-grey': projectStatus().name() === 'Complete', 'label-yellow': projectStatus().name() === 'Unsuccessful'}"/&gt; </code></pre> <p>This way you're not putting style information in your JS model code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload