Skip to content

There is no way to use KO's "special context properties" for the collection option #13

@pkmccaffrey

Description

@pkmccaffrey

Currently, the collection option only knows how to deal with strings, as viewModel[collection] is used to access the desired array. This works fine, except in cases where you need to use Knockout's $data context property as the collection option. While using $data as the collection may sound strange, I have a use case for it (I'm inside a KO with binding, and at that point, $data is my collection).

My proposal to fix this is to make the binding capable of accepting references to the collection itself for the collection option. The diff for my commit is at the bottom - I've done some testing, and things seem to work fine. I'll be testing further tomorrow - just wanted to get the idea out there.

Proposed Example HTML 1 (reference collection by name)

<table class="table-ordered">
    <thead>
        <tr>
            <th data-bind="orderable: { collection: 'Collection', field: 'FieldName' }">Column Header</th>
            (rest of table header here)
        </tr>
        <tbody data-bind="foreach: $data">
            (table body context here)
        </tbody>
    </thead>
</table>

Proposed Example HTML 2 (reference collection by object)

<table class="table-ordered">
    <thead>
        <tr>
            <th data-bind="orderable: { collection: Collection, field: 'FieldName' }">Column Header</th>
            (rest of table header here)
        </tr>
        <tbody data-bind="foreach: $data">
            (table body context here)
        </tbody>
    </thead>
</table>

Proposed Example HTML 3 (reference collection by $data property

<table class="table-ordered" data-bind="with: Collection">
    <thead>
        <tr>
            <th data-bind="orderable: { collection: $data, field: 'FieldName' }">Column Header</th>
            (rest of table header here)
        </tr>
        <tbody data-bind="foreach: $data">
            (table body context here)
        </tbody>
    </thead>
</table>

Proposed Script

Commit Diff

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions