It’s probably best to explain with an example. This is an example linked from the Knockout blog on how to use $parent. However, in this case, $parent is apparently the same as $root, so I’m having trouble seeing the benefit.
Where it says $parent.lastUpdated, I’d like to instead access the observable array, this.products. I realize I could say $root.products. However, I’d like to generalize this, rather than write it explicitly for every list.
I will actually be using this in conjunction with ko.contextFor.
You can’t get access (generically) to the observableArray that is being looped over directly inside of the foreach loop.
$rootand$parentis about the scope, so it will just give you the object that contains the observableArray.However, you can take advantage of this by ensuring that the scope is your observableArray by using the
withbinding.If you just need the underlying array, then you can do:
If you really need the observableArray, then you need to make sure that it has not been unwrapped (in the above example
$datais unwrapped already). To do this, you can normalize the name of the array and look for it in your template like:Here is a sample: http://jsfiddle.net/rniemeyer/T6JvV/
If you can’t live with the extra
div, then you can certainly use the containerless control-flow bindings like: