1

I have a collection of objects. The collection is a observableArray because I want to update my view when the collection changes in size.

I do not want to make the fields in the collections items observable because they are almost always constants and I do not want the overhead and ugly syntax of observables.

But there is that one extremely rare operation that need to update the values of ALL the elements in the list.

I don't mind to have a performance penalty in this case (a few seconds), but I don't know how to "rebind" the entire list and refresh the bindings.

I tried using valueHasMutated on the list, but do not have the desired effect. I made a fiddle with a simplified view of my problem.

class Item
    constructor: (value) ->
        @value = value

class ItemsList
    constructor: () ->
        @items  = ko.observableArray (new Item(1) for number in [1..10])

    rareOperation: () =>
        item.value = item.value + 1 for item in @items()
        @items.valueHasMutated()

ko.applyBindings( new ItemsList() )

http://jsfiddle.net/EXkWg/2/

I know that it is not the normal use-case of Knockout, and the right answer is to use observables, but in this case I prefer something else.

Fernando
  • 2,131
  • 3
  • 27
  • 46
  • " but in this case I prefer something else." - would you mind elaborating why? If you have a good reason - I want to know. – Benjamin Gruenbaum Sep 06 '13 at 22:33
  • First, lot's of observables must have performance and memory issues (the real case are not only 10 items). Second, there is a lot of code that will need change if I use observables... Third, the setter syntax is awful (don't blame me.. I understand the reason behind the setters, but still is very weird)... and Fourth, because I would like to know how to do it :) – Fernando Sep 06 '13 at 22:38
  • 2
    As stated by other people this has already been answered http://stackoverflow.com/questions/13231738/refresh-observablearray-when-items-are-not-observables – Sergiu Paraschiv Sep 06 '13 at 22:48
  • thanks for sharing the link, – sairfan Feb 07 '19 at 22:35

0 Answers0