BorisMoore / jsviews

Interactive data-driven views, MVVM and MVP, built on top of JsRender templates
http://www.jsviews.com/#jsviews
MIT License
855 stars 130 forks source link

Firefox problem: <select> initial selection failing #330

Closed laurensdijkstra closed 8 years ago

laurensdijkstra commented 8 years ago

In a scenario where one has two tabs that contain select boxes, switching between the tabs (with an observable property) displays correctly the initially selected values. But after selecting a new option in a select box and then switching to the other tab and back, the value is not updated.

BorisMoore commented 8 years ago

I'm not sure what problem you are having. This seems to work correctly for me:

<script id="myTmpl" type="text/x-jsrender">
  {^{tabs tabCaption="first" width="200px"}}
    one
  {{else tabCaption="second"}}
    <select data-link="selectedID">
      <option value="0">Choose a person to edit</option>
      {^{for people}}
        <option data-link="{:name} value{:ID} selected{:ID === ~root.selectedID}">ss</option>
      {{/for}}
    </select> 
  {{/tabs}}
</script>

<script>
var people = [
  {
    ID: "Ad0",
    name: "Adriana"
  },
  {
    ID: "Ro0",
    name: "Robert"
  }
];

var myTmpl = $.templates("#myTmpl");

myTmpl.link("#tabsView", {
  selectedID: "Ro0",
  people: people,
});

</script>

Does it work for you? If there is still an issue, you would need to create a simple jsfiddle example so I can see what you are trying to do, and how you are doing it...

laurensdijkstra commented 8 years ago

Yes this works for me aswell. I was not aware of the existence of a tabs "plugin". So maybe that is why this does work.

Excuse me for not giving a clearer example but reproducing the behaviour in a fiddle was taking too much time.

My code looks something like this:

{^{for project.scenarios}}
    <tr data-link="selected{:~root.selectedScenario^id === id}">
        <td>
            <label>
                Visible
                <input type="checkbox" name="visibility" data-link="visible" />
            </label>
        </td>
        <td><a href="javascript:void(0)" data-link="{:name} {on ~selectScenario}"></a></td>
    </tr>
{{/for}}

{^{for selectedScenario^instances}}
<tr data-link="selected{:~isSelected(#data)}">
        <label>
            Template
            <select data-link="templateId">
                {^{for ~root.project.templates ~templateId=templateId}}
                    <option data-link="{:name} value{:id} selected{:id === ~templateId}"></option>
                {{/for}}
            </select>
        </label>
{{/for}}

A declarative click handler on the scenario selector calls a helper function selectScenario. This in turn changes viewModel.selectedScenario (observably). A scenario contains a list of instances and an instance has a templateId property. viewModel.project has a templates array that is matched to the templateId to determine the selected template in the