mbest / knockout-repeat

REPEAT binding for Knockout
http://mbest.github.io/knockout-repeat/
131 stars 18 forks source link

support `_destroy` property #11

Closed doughsay closed 11 years ago

doughsay commented 11 years ago

If the collection you're iterating over is an object that has either a property or an observable property called _destroy set to true, it should not render that element.

This is supported by default using the built-in foreach binding in knockout, so it seems to make sense to support it here.

See here for reference: http://knockoutjs.com/documentation/observableArrays.html#destroy_and_destroyall_note_usually_relevant_to_ruby_on_rails_developers_only

mbest commented 11 years ago

I'd rather not include that in the Repeat binding. One of the main advantages of this binding is its speed, which would be compromised by having to filter out array items.

It would be pretty easy to filter out the destroyed items yourself. Here's a function that does it:

function noDestroyed(array) {
    return ko.utils.arrayFilter(ko.unwrap(array), function(item) {
        return item == null || !ko.unwrap(item._destroy);
    });
}

You could then bind using repeat like this:

<div data-bind="repeat: noDestroyed(myArray)">...</div>
doughsay commented 11 years ago

Thanks, that does make sense. You could implement that function as a computed observable array as well, making the bind syntax look identical to if you were using a regular observable array.