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

multiselect not updating when linked array changes #450

Closed johan-ohrn closed 3 years ago

johan-ohrn commented 4 years ago

Please have a look at this fiddle. (code below) Notice that the select element without the copy converter does not update.

I found that the select element does not update it's selected values when you observably modify the selectedValues array.

I followed the code flow and found this line in jquery.views.js: 623: if (change = change || targetVal !== sourceValue) { As a workaround I realized I can just create a converter that copies the array so that targetVal and sourceValue are different array instances. It works but it's not very obvious.

html:
<div id="ph"></div>

js:
$.views.converters({
  copy: function(arr){return arr.slice();}
})

var vm = {
  selectedValues: ["value2"]
};

$.templates("\
    with copy converter \
  <select multiple data-link='{copy:selectedValues:}'>  \
    <option value='value1'>1</option>   \
    <option value='value2'>2</option>   \
  </select>     \
  without copy converter \
  <select multiple data-link='selectedValues'>  \
    <option value='value1'>1</option>   \
    <option value='value2'>2</option>   \
  </select>     \
").link("#ph", vm);

setTimeout(function() {
  console.log("blah")
    $.observable(vm.selectedValues).refresh(["value1"]);
},1000)
BorisMoore commented 4 years ago

Thanks Johan. I'll look into it...

BorisMoore commented 3 years ago

Hi Johan, It's been a long time and I did in fact find a fix for your issue above, but also discovered some slightly related problems with the checkboxgroup tag. I have been working towards a new update which will include your the fix for your issue. But it's taking a while.... If you can wait for your fix, that's good, but if you would hope to get it sooner, let me know and I'll probably create an intermediate update with just your fix...

johan-ohrn commented 3 years ago

Hi, Good to hear. We will wait for the fix since we have a workaround for the problem. Thanks

BorisMoore commented 3 years ago

This update should fix your issue, along with a number of other small issues I found, particularly concerning <select>, multiselect, and {{checkboxgroup}}, (and some elsewhere...)

jsviews8e.zip

Let me know if it works for you... Thanks!

BorisMoore commented 3 years ago

Update - I am checking another possible issue, so probably better to wait before testing jsviews8e, above. I'll keep you posted if there is a corrected version coming...

BorisMoore commented 3 years ago

OK, I think this update is good. Let me know if you find any issues in your environment' Thanks!

jsviews8f.zip

BorisMoore commented 3 years ago

I'm moving towards publishing the above a v1.0.8. Of course, if you do find an issue before I actually publish, then I'll put it on hold, but I am pretty sure that for known issues it is good...

johan-ohrn commented 3 years ago

I probably won't be able to test it in the coming days. I'm stuck with some major pita I must attend to first. I'll write back here though when I have tried it. I'm sure it will work tho :)

BorisMoore commented 3 years ago

Thanks Johan, yes, that's fine. I'll go ahead with what we have. Worst case we'll need a v1.0.9 subsequently. (Which at some point will happen anyway...) Thanks for letting me know status so rapidly...!

BorisMoore commented 3 years ago

Resolved in v1.0.8