Ok, the title may be a bit confusing so I'll explain...
I'm writing a custom binding to which I'll pass an observableArray. This observableArray is populated asynchronously and the elements are pushed in one by one.
The problem is that my custom binding is called (the update method) each time the observableArray is mutated. Which makes sense but which isn't helpful in this instance because it means that the first element gets rendered n times, where n is the length of the observableArray, and the second element gets rendered n-1 times, with only the nth element being rendered once.
Can anyone explain a neat method of having the custom binding only do something when either
- the observableArray is fully populated, or
- when an element has been added which has not already been rendered by the custom binding?
I can think of a couple of ways around this using an additional property/observable as a flag on the parent view-model which says "fully populated, you can render the items now" or a property on each of the elements which says "you've already rendered me". However, these are both awkward, particularly as the objects inside the observableArray also have an observableArray property.
Isn't there a better Knockout/MVVM solution to this problem?
UPDATE: For clarity, what I'm building on top of is something like this
<domElmnt data-bind="myBinding: { collection: TypeGroups }" />
where
TypeGroups = ko.observableArray();
and where the elements contained in TypeGroups are all instances of another view-model with observable properties.
Each time I call TypeGroups.push(obj) the custom binding is called once again.
new Date()to check if it actually rerender things. Something like<span data-bind="text: new Date()"></span>- Loïc Faure-Lacroixforeachbinding? - Michael Bestforeachin place until we found IE9 was broken, and that's the very reason that I've moved to a custom binding. - awj