Currently I'm bound to plain array. Values of the array can be changed by external components.
Is there any possibility to send notification that value has changed and re-render DOM tree?
I can't use observables, thus valueHasMutated
is not a solution, array is very large and contains lot of complex objects in it.
I'm assuming you're using Knockout.js since you tagged it. Therefore, you can use ko.ObservableArray() and bind it to the DOM.
See this link
If I understand correctly, the array is a member of the ViewModel (which you have control over), but you cannot change it into an
observableArray
because things outside of your control modify the array, using plain array syntax. Also, you can take some action after these black-box functions run, to notify the model that the array may have been mutated.We can do this. Define an (internal)
observableArray
and define a public property on the ViewModel that wraps it. That gives you access to theobservableArray
using ordinary array syntax. However, changes to individual (non-observable
) elements of an array do not send notifications, so you will need to provide a call to thevalueHasMutated
method of the internalobservableArray
, to be called whenever you make changes.Our app can use the
plainArray
property just like a regular array. When updates are made, callarrayHasMutated
.Try it out: http://jsfiddle.net/4jogh3k5/1/
If you can pass another object, instead of the original array, to the consumers then you can use
defineProperty
to wrap mutations.Obviously this only works when you change the value at an index. If your array has objects and you change one of those objects directly, like
original[0].property = "value"
then you would need to extend this technique to visit every object in the array and create an equivalent structure of wrapper objects.