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

Bugfix: Error when calling ko.bindingEvent.subscribe( 'childrenComplete' ) before applyBindings has reached the node. #2584

Open
Griffork opened this issue Jan 9, 2022 · 0 comments
Milestone

Comments

@Griffork
Copy link

Griffork commented Jan 9, 2022

I wanted to know when a given node had finished being initialised (along with it's children) and tried to call it before applyBindings had reached the node in question, and proceeded to get a syntax error in knockout core instead of setting up a subscription properly.

My code:

let completed = false;
var sub = (ko as any).bindingEvent.subscribe(node, 'childrenComplete', function () {
if (sub) {
	sub.dispose();
}
completed = true;
resolve();
}, null, { notifyImmediately: true });
if (completed) sub.dispose();

The error occurs on line 3442 of knockout-3.5.1.debug.js:

    ko.bindingEvent = {
        childrenComplete: "childrenComplete",
        descendantsComplete : "descendantsComplete",

        subscribe: function (node, event, callback, context, options) {
            var bindingInfo = ko.utils.domData.getOrSet(node, boundElementDomDataKey, {});
            if (!bindingInfo.eventSubscribable) {
                bindingInfo.eventSubscribable = new ko.subscribable;
            }
            if (options && options['notifyImmediately'] && bindingInfo.notifiedEvents[event]) { //Error because bindingInfo.notifiedEvents is undefined.
                ko.dependencyDetection.ignore(callback, context, [node]);
            }
            return bindingInfo.eventSubscribable.subscribe(callback, context, event);
        },

I changed the line as follows and no longer get an error:

if (options && options['notifyImmediately'] && bindingInfo.notifiedEvents && bindingInfo.notifiedEvents[event]) {

I believe that this API isn't supported externally, but I would still like it if this is fixed so I can use it regardless (understanding that it may not be supported in the future).

@mbest mbest added this to the 3.5.2 milestone Mar 8, 2023
@mbest mbest added the type: bug label Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants