Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having a condition inside the sortable item template that can exclude the item creates error #10

Closed
Mark-Robinson opened this issue Mar 21, 2012 · 2 comments

Comments

@Mark-Robinson
Copy link

See http://jsfiddle.net/unklefolk/VQndj/ based on the simple example.

The condition:

ko if: $data.Name !== 'Fix car'

.. causes a certain item to be excluded. However, this causes an error:

Uncaught TypeError: Cannot read property '__ko__1332343640095' of null

This is because in afterRender, element.parentNode is null.

    afterRender: function (elements, data) {
        ko.utils.arrayForEach(elements, function (element) {
            if (element.nodeType === 1) {
                ko.utils.domData.set(element, itemKey, data);
                ko.utils.domData.set(element, parentKey, ko.utils.domData.get(element.parentNode, listKey));
            }
        });
    },

The following seems to fix the error but not sure if this is the correct fix:

    afterRender: function (elements, data) {
        ko.utils.arrayForEach(elements, function (element) {
            if (element.nodeType === 1 && element.parentNode) {
                ko.utils.domData.set(element, itemKey, data);
                ko.utils.domData.set(element, parentKey, ko.utils.domData.get(element.parentNode, listKey));
            }
        });
    },
@rniemeyer
Copy link
Owner

@Mark-Robinson - thanks for reporting this one. I am going to explore fixes for it. Looks like a tricky one, as the if binding removes the original element from the DOM to use as a template. The meta-data gets attached to this element (which no longer has a parent) and has already been used as a template for the actual content. Looks to be a bit of a challenge to get the meta-data set before the "if" binding does its thing.

To be safe for now, you might want to either wrap each item in an element (put your "if" on a div and make sure that when there is no content, it does not mess up your display) or use "visible" instead of "if".

@rniemeyer
Copy link
Owner

I don't see a good way to fix this one at this time. I will keep it in mind, if the situation changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants