Skip to content

Adding event listeners before or after init? #536

Closed
@oller

Description

@oller

Hello all, I'm having a bit of a conundrum binding event listeners to querybuilder.

As per my code below, I'm adding the.on() event listeners chained before the init of querybuilder(). This works in as much as all the events are detected correctly, including afterInit.

        const builder = view.$el.find("#builder");

        builder.on("afterAddGroup.queryBuilder", (event, group) => {
            console.log("on after add group");
            // Each time a group is added, it rewrites the dom, always ensure we
            // apply the applicable template options back to the group dropdown
            view.updateTemplateList(group);
            view.applyPermittedGroupNegation(group);
            view.updateSQLPane();
        })
        .on("afterDeleteGroup.queryBuilder", () => {
            console.log("on after delete group");
            view.applyPermittedGroupNegation();
            view.updateSQLPane();
        })
        .on("afterUpdateGroupCondition.queryBuilder afterUpdateRuleFilter.queryBuilder afterUpdateRuleOperator.queryBuilder afterUpdateRuleValue.queryBuilder", () => {
            console.log("values updated");
            view.updateSQLPane();
        })
        .on("afterMove.queryBuilder", (event, node) => {
            console.log("after drag move");
            if (node.constructor.name === "Group") {
                // User has dragged a group
                view.applyPermittedGroupNegation(node);
            }
            view.updateSQLPane();
        })
        .on("afterInit.queryBuilder", () => {
            console.log("after init");
            view.updateTemplateList(view);
        })
        .queryBuilder({
            rules: view.getDefaultRoot(),
            allow_empty: true,
            plugins: {
                "not-group": null,
                sortable: { icon: "icon-button icon-menu" }
            },
            display_empty_filter: false,
            filters: view.filters,
            icons: {
                error: "icon-button bg-seagreen c-white icon-infoFilled"
            },
            templates: {
                rule: templateRule(),
                filterSelect: templateFilter(),
                operatorSelect: templateOperator(),
                group: templateGroup()
            }
        });

The issue I'm having is that in some of the event listeners I'm trying to validate the query (on the fly validation) and if valid updating a SQL query preview. These all error with the following error. It's as if the querybuilder was initialised with no filters, when I know this is not the case, as that view.filters property is populated before the init, there's no async issues here, and before adding this on the fly validation, the builder was working perfectly.

From going through the stack trace, data is returning undefined, so it seems a new instance of the builder is being initialised, a new instance which won't have filters etc. defined. I'm unsure why this is the case though, as I'm following the same pattern in the demo of $('selector').queryBuilder('method'); to run a method on an already initialised builder.

if (!data) {
this.data('queryBuilder', new QueryBuilder(this, options));
}

Uncaught Error: Missing filters list

Utils.error | @ | query-builder.js?49d5:3520
QueryBuilder.checkFilters | @ | query-builder.js?49d5:558
QueryBuilder | @ | query-builder.js?49d5:162
$.fn.queryBuilder | @ | query-builder.js?49d5:3667
getSQL | @ | builderaddedit.view.js?706c:382

I've tried chaining the .on() event listeners after the queryBuilder() but then half of them don't fire, for example binding afterInit after you've initialised the builder is pretty pointless.

It's a pretty standard use-case, so I'm sure i'm not trying the impossible. Is there a suggested way I should be listening to these events?

Apologies if i'm doing something silly, I'm following the docs as closely as possible, I'm a bit stuck! A fantastic library and set of docs otherwise, I've got a lot done before hitting this issue!

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIdentified bug which needs a fix

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions