jQuery-QueryBuilder icon indicating copy to clipboard operation
jQuery-QueryBuilder copied to clipboard

Intercept only change event triggered by the GUI

Open fabricejumarie opened this issue 5 years ago • 4 comments

Hi,

I use JQuery query builder in a angular child component and I meet many troubles with events.

When I initialize the query builder before displaying on my web page, I get the JSON data which contains the definition of the rule (condition, filters, operator...). That is not the problem because everything works fine so far.

The problem is I want to intercept all modification performed by the user via the GUI and only these modifcations and not the modification brought by the set_rule method.

  1. Do you know a mean so that setting rule with JSON (set_rule method) do not trigger afterCreateRuleInput event?
  2. Does it exist a property on event handler parameters which allows knowing change origin (rule initialization via set_rule or user via the GUI)?
  3. How to retrieve the new JSON data in the event Handler?

Regards

Fabrice

fabricejumarie avatar Sep 20 '18 15:09 fabricejumarie

To retrieve the rules you need the getRules method (convenient name :-D ) https://querybuilder.js.org/#method-getRules


There is currently no way to distinguish or stop events when the setRules method is called. Will consider it. It will certainly not be removable, because plugins use these events to render.

BTW you can do it yourself : set a global flag before calling setRules and remove it after, and use this flag in your events handlers.

mistic100 avatar Sep 20 '18 17:09 mistic100

To retrieve the rules you need the getRules method (convenient name :-D ) https://querybuilder.js.org/#method-getRules

There is currently no way to distinguish or stop events when the setRules method is called. Will consider it. It will certainly not be removable, because plugins use these events to render. BTW you can do it yourself : set a global flag before calling setRules and remove it after, and use this flag in your events handlers.

Yes, calling getRules method works after changing some piece of code. Before, I initialized query builder rule by calling setRule with JSON object as parameter. The call to SetRule method triggered events and I got error in the event Handler by calling getRule.

Now, I initialize query builder rule directly when I create the query builder as shown below :

$('#builder').queryBuilder({
      plugins: [...],
      filters: this.filters,
      rules: JsonRule
    });

I think there is a issue when you call getRule in an event Handler triggered by the setRule function.

fabricejumarie avatar Sep 21 '18 08:09 fabricejumarie

"I got an error" is not really helpful.....

But when "setRules" is processing the model is being constructed (step by step), no surprise "getRules" behaves incorrectly, it's conceptually wrong to do it at this moment, hence the flag system you need or I will add in the next version. You don't get the error when setting init "rules" (which calls "setRules" by the way) because your events handlers are not attached yet.

mistic100 avatar Sep 21 '18 20:09 mistic100

Yes I have chosen to initiate rule sooner to do it before event handlers be attached.

Regarding errors I got before, it was something like that : cannot find foreach from undefined, I could give you the right message on Monday and yes I have guessed the error was raised because getRule was called during setRule process. But setRule triggers events and I call getRule in the event handler so I hadn't control on callback "chain".

But your suggestion was helpful and everything works fine now

fabricejumarie avatar Sep 21 '18 21:09 fabricejumarie