0
votes

I am trying to convert this knockout code to JSViews: here

I can make it work for the first iteration, but clicking on a color does not update the array and therefore does not add the 'js-active' class. I know the reason is because I am not observing for that change, but I cant seem to see how to connect the two.

I have taken the two separate ko.observbles and joined them in to one:

var colorPickerData = {
selectedColor: "Red",

materialColors: [
    {
        color: "Red",
        variations: [
            {
                weight: 50,
                hex: "#FFEBEE"
....

and here is the template:

 <script id="color-picker-template" type="text/x-jsrender">
    <div class="material-color-picker">
        <div class="material-color-picker__left-panel">
            <ol class="color-selector"> 
                {^{for materialColors ~paletteColor=selectedColor }}
                <li>
                    <input name="material-color" type="radio" data-link="id{:'materialColor' + #getIndex() } checked{:~paletteColor} value{:color}">
                    <label data-link="for{:'materialColor' + #getIndex()} title{:color} css-color{:variations[4].hex }"></label>
                </li>
                {{/for}}
            </ol>
        </div>
        <div class="material-color-picker__right-panel">
           {^{for materialColors ~paletteColor=selectedColor}}
            <div data-link="class{:(color == ~paletteColor)? 'js-active color-palette-wrapper': 'color-palette-wrapper' }">

                <h5 class="color-palette-header" data-link="text{:color}"></h5>
                <ol class="color-palette">
                    {^{for variations}}
                    <li id="clipboardItem" class="color-palette__item" data-link="data-clipboard-text{:hex } css-background-color{:hex }">
                        <span data-link="text{:weight}"></span>
                        <span data-link="text{:hex}"></span>

                    </li>
                    {{/for}}
                </ol>

            </div>
            {{/for}}
        </div>
    </div>

here is the linking code:

$.templates("#color-picker-template").link("#color-wrapper", colorPickerData, true);

Can anyone tell me how I can observe for the changes to selectedColor?

1

1 Answers

1
votes

I forked your CodePen here and completed a port to JsViews.

I simplified some of the data-linking markup to reduce perf cost of unnecessary data binding.

For your question about two-way binding to radio buttons:

You can do

{^{radiogroup selectedColor}}
  {{for materialColors}}
    <li>
        <input name="material-color" id="materialColor{{:#index}}" value="{{:color}}" type="radio" />
        <label ...></label>
    </li>
  {{/for}}
{{/radiogroup}}

or

{{for materialColors ~paletteColor=selectedColor}}
  <li>
    <input name="material-color" id="materialColor{{:#index}}" value="{{:color}}" type="radio" data-link="~paletteColor"/>
    <label ...></label>
  </li>
{{/for}}

or - if you keep your full data-linking approach:

<input name="material-color" type="radio" data-link="id{:'materialColor' + #getIndex() } value{:color} {:~paletteColor:}">

See {{radiogroup}} and Data-linked radio buttons.