brianmhunt / knockout-fast-foreach

Foreach. Faster. For Knockout.
MIT License
59 stars 18 forks source link

Rendering error when binding the same data to multiple parts of the document #25

Closed cervengoc closed 9 years ago

cervengoc commented 9 years ago

See this fiddle: http://jsfiddle.net/48hrb9cj/5/

I implemented a method called refill for observable arrays. This method clears the array and pushes all the given elements into it while keeping the array silent (valueWillMutate/valueHasMutated).

Consider the following circumstances:

  1. I have more than one place where I bind the same observable array (3 in the example)
  2. On a button click, I pop a value, then refill the array with values.
  3. The values with which I refill the array contains at least one from the original value set (number 1 in the example).

In this scenario something goes crazy, I can see that always the last number which I pushed to the array (number 2 in example) gets repeated, and number or repetitions = number of usages of fastforEach binding on the same data (3 times in the example).

Hope you can find it out, I was looking at the code for half an hour without any idea.

(I copied the latest source code for simplicity.)

brianmhunt commented 9 years ago

@cervengoc My first thought is that that mucking with the array change subscriptions will cause the arrayChange events to not fire correctly.

What happens if you clear the array and push the elements without keeping the array silent?

cervengoc commented 9 years ago

Wooh I think I've found the issue, and it came with my code. It looks like ko is sending the same arrayChange items to every subscriber, so when I manipulate it to create a batch addition, then I manipulate the same instance at every binding. I'm gonna fix it soon, and I'll try to put together a test for this use case, at most tomorrow.

brianmhunt commented 9 years ago

@cervengoc – Are you up for putting a PR in for this?

cervengoc commented 9 years ago

I've actually already done it. I even wondered why you didn't react. Probably I missed something, I'll check later today when I'm at computer. Please see my fork till then.