Knockout.Extensions UniqueId Binding

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<p>
    To see the generated ID's for the Html Elements below, inspect the elements using firebug
    or the build in developer tools in Internet Explorer.
</p>
<!-- 
    The "uniqueId" here tells the binding what attribute should be updated on the element with 
    the Id.
    The "uniqueIdFor" explicitly tells the binding which field in the view model should be used
    to figure out the Id.
-->
<label data-bind="uniqueId: ['for'], uniqueIdFor: personName">Person Name</label>

<!-- 
    The "uniqueId" here tells the binding that it should apply the generated Id to the HTML
    element in the default way.  This means, the Id attribute of the element will be set with 
    the generated Id, and the Id will be generated based off the view model field that is 
    bound to the "value", "selectedOptions", or "checked" bindings.
-->
<input type="text" data-bind="value: personName, uniqueId: true" /><br />

<!-- ko foreach: hest -->
<!-- ko foreach: $root.addresses -->
    <h3>Address</h3>
    <label data-bind="uniqueId: ['for'], uniqueIdFor: street">Street</label>
    <input type="text" data-bind="value: street, uniqueId: true" /><br />

    <label data-bind="uniqueId: ['for'], uniqueIdFor: suburb">Suburb</label>
    <input type="text" data-bind="value: suburb, uniqueId: true" /><br />

<!-- /ko -->
<!-- /ko -->

<script type="text/javascript">

</script>

CSS

p { margin-bottom:10px; }
label { font-weight:bold; display:inline-block; width:100px; }
h3 { margin-top:5px; font-weight:bold; }

JavaScript

// This binding calculates the field path one must take to get from the root of the view model
// to the field that is bound to the "value", "checked", or "selectedOptions" binding of the current element.
// It then uses this field path as a deterministic unique identifier for the current element.
//
// Limitations:
// 1) This binding generates a unique Id per field in the view model; therefore, if you have more than one HTML element bound
//    to the same field in the view model, it is possible to have a duplicate ID across these elements if both elements are using
//    this binding to set their ID attribute.
// 2) This binding cannot generate IDs for elements bound to NULL fields.  Observable fields with a value of NULL are fine,
//    but not straight out NULL fields.
//
// Assumptions
// 1) The very basic field path caching in this binding assumes that elements that bind to the same field will be grouped together, 
//    and that elements that bind to the same binding context will be grouped together.
ko.bindingHandlers.uniqueId = new function () {

    var _cachedContext;
    var _cachedFieldPathParts = [];
    var _cachedValueField;
    var _cachedValueFieldPath;

    this.init = function (element, bindingOptionsAccessor, allBindingsAccessor, viewModel, bindingContext) {

        var allBindings = allBindingsAccessor();
        var valueField;
        var attributesToSet = ["id"];
        var bindingOptions = bindingOptionsAccessor();

        // Figure out which element attributes need to be set with the Id generated by this binding.
        if (bindingOptions === false) {
            return;
        }
        else if (bindingOptions instanceof Array && bindingOptions.length > 0) {
            attributesToSet = bindingOptions;
        }
        else if (typeof bindingOptions === "string") {
            attributesToSet = [bindingOptions];
        }

        // Retrieve the value field for the element that is being bound currently, if it has one.
       ...