Issue with handling events

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Issue with handling events

Harry Devine
Everyone,

I have a situation where I want the user to select a row in a grid by either right-clicking and getting a context menu option or by double-clicking the row.  In both instances the same function in my module is called.  The grid exists in a dijit Dialog. When the function is called, I put some text onto the main page and close the dialog by calling its hide method. The next time I bring up that dialog and try the context menu or double-click, I get the module function called twice.  The next time will be 3 times, etc.  I think that the on handler is creating one instance for the context menu and 1 for the double-click, and not un-registering them when the dialog is closed.  I tried on.once, but that won't work as if I happen to try the context menu route and cancel it by clicking away, the double-click will never get called since once will stop the events after either once fires.

Any ideas?  I guess if I could find a way to unregister the on handler when I hide the dialog, that would be the best route, but I can't find out how to do that.  But I'm open to any other ideas.  I can try to put together a JSFiddle if my explanation doesn't make sense.

Thanks!
Harry

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Issue with handling events

dylanks
Are you explicitly creating and destroying the dgrid instance with the
open and close of each dialog?

It sounds like you're keeping the dialog open, but adding the event
handler each time you open it up. So you'll want to get a reference to
the event handler when you create it and destroy it when you close the
dialog, or only create the event handler if it's not already been
created, when opening the dialog. There are scenarios where one approach
is better than the other.

Another option is to create the event handler outside the context of the
dialog, using event delegation based on the expected class names for the
structure, and just have it available even if the grid doesn't exist.

When you register an event handler, you can get a reference to it. So
you can then remove that handler when appropriate.

Regards,
-Dylan

on 6/24/16, 05:44 (GMT-07:00) Harry Devine said the following:

> Everyone,
>
> I have a situation where I want the user to select a row in a grid by
> either right-clicking and getting a context menu option or by
> double-clicking the row.  In both instances the same function in my
> module is called.  The grid exists in a dijit Dialog. When the function
> is called, I put some text onto the main page and close the dialog by
> calling its hide method. The next time I bring up that dialog and try
> the context menu or double-click, I get the module function called
> twice.  The next time will be 3 times, etc.  I think that the on handler
> is creating one instance for the context menu and 1 for the
> double-click, and not un-registering them when the dialog is closed.  I
> tried on.once, but that won't work as if I happen to try the context
> menu route and cancel it by clicking away, the double-click will never
> get called since once will stop the events after either once fires.
>
> Any ideas?  I guess if I could find a way to unregister the on handler
> when I hide the dialog, that would be the best route, but I can't find
> out how to do that.  But I'm open to any other ideas.  I can try to put
> together a JSFiddle if my explanation doesn't make sense.
>
> Thanks!
> Harry
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Co-Founder, Dojo Toolkit
CEO, SitePen, Inc.  http://www.sitepen.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Issue with handling events

Harry Devine
What I just got to work right before your response was the remove method.  I did this and it seemed to work in both event instances:

var signal = on(grid, ".dgrid-row:contextmenu, .dgrid-content .dgrid-cell:dblclick", function(evt) {
  evt.preventDefault();
  row = grid.row(evt);
  activeItem = row && row.data;
  if (evt.type == "dblclick") {
    mod.confirmResolution(grid.row(evt).data.outcome);
    signal.remove();
  }
});

var menu = new Menu({
  targetNodeIds: [grid.domNode]
});
menu.addChild(new MenuItem({
  label: "Use as resolution?",
  onClick: function() {
    if (activeItem) {
      mod.confirmResolution(activeItem.outcome);
      signal.remove();
    }
  }
}));

Thanks for the help!
Harry

From: "Dylan Schiemann" <[hidden email]>
To: [hidden email]
Sent: Friday, June 24, 2016 9:10:55 AM
Subject: Re: [Dojo-interest] Issue with handling events

Are you explicitly creating and destroying the dgrid instance with the
open and close of each dialog?

It sounds like you're keeping the dialog open, but adding the event
handler each time you open it up. So you'll want to get a reference to
the event handler when you create it and destroy it when you close the
dialog, or only create the event handler if it's not already been
created, when opening the dialog. There are scenarios where one approach
is better than the other.

Another option is to create the event handler outside the context of the
dialog, using event delegation based on the expected class names for the
structure, and just have it available even if the grid doesn't exist.

When you register an event handler, you can get a reference to it. So
you can then remove that handler when appropriate.

Regards,
-Dylan

on 6/24/16, 05:44 (GMT-07:00) Harry Devine said the following:

> Everyone,
>
> I have a situation where I want the user to select a row in a grid by
> either right-clicking and getting a context menu option or by
> double-clicking the row.  In both instances the same function in my
> module is called.  The grid exists in a dijit Dialog. When the function
> is called, I put some text onto the main page and close the dialog by
> calling its hide method. The next time I bring up that dialog and try
> the context menu or double-click, I get the module function called
> twice.  The next time will be 3 times, etc.  I think that the on handler
> is creating one instance for the context menu and 1 for the
> double-click, and not un-registering them when the dialog is closed.  I
> tried on.once, but that won't work as if I happen to try the context
> menu route and cancel it by clicking away, the double-click will never
> get called since once will stop the events after either once fires.
>
> Any ideas?  I guess if I could find a way to unregister the on handler
> when I hide the dialog, that would be the best route, but I can't find
> out how to do that.  But I'm open to any other ideas.  I can try to put
> together a JSFiddle if my explanation doesn't make sense.
>
> Thanks!
> Harry
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Issue with handling events

dylanks
Sure, using remove seems fine. It wasn't clear that this would work for
you without seeing the original code!

on 6/24/16, 06:21 (GMT-07:00) Harry Devine said the following:

> What I just got to work right before your response was the remove
> method.  I did this and it seemed to work in both event instances:
>
> var signal = on(grid, ".dgrid-row:contextmenu, .dgrid-content
> .dgrid-cell:dblclick", function(evt) {
>   evt.preventDefault();
>   row = grid.row(evt);
>   activeItem = row && row.data;
>   if (evt.type == "dblclick") {
>     mod.confirmResolution(grid.row(evt).data.outcome);
>     signal.remove();
>   }
> });
>
> var menu = new Menu({
>   targetNodeIds: [grid.domNode]
> });
> menu.addChild(new MenuItem({
>   label: "Use as resolution?",
>   onClick: function() {
>     if (activeItem) {
>       mod.confirmResolution(activeItem.outcome);
>       signal.remove();
>     }
>   }
> }));
>
> Thanks for the help!
> Harry
> ------------------------------------------------------------------------
> *From: *"Dylan Schiemann" <[hidden email]>
> *To: *[hidden email]
> *Sent: *Friday, June 24, 2016 9:10:55 AM
> *Subject: *Re: [Dojo-interest] Issue with handling events
>
> Are you explicitly creating and destroying the dgrid instance with the
> open and close of each dialog?
>
> It sounds like you're keeping the dialog open, but adding the event
> handler each time you open it up. So you'll want to get a reference to
> the event handler when you create it and destroy it when you close the
> dialog, or only create the event handler if it's not already been
> created, when opening the dialog. There are scenarios where one approach
> is better than the other.
>
> Another option is to create the event handler outside the context of the
> dialog, using event delegation based on the expected class names for the
> structure, and just have it available even if the grid doesn't exist.
>
> When you register an event handler, you can get a reference to it. So
> you can then remove that handler when appropriate.
>
> Regards,
> -Dylan
>
> on 6/24/16, 05:44 (GMT-07:00) Harry Devine said the following:
>> Everyone,
>>
>> I have a situation where I want the user to select a row in a grid by
>> either right-clicking and getting a context menu option or by
>> double-clicking the row.  In both instances the same function in my
>> module is called.  The grid exists in a dijit Dialog. When the function
>> is called, I put some text onto the main page and close the dialog by
>> calling its hide method. The next time I bring up that dialog and try
>> the context menu or double-click, I get the module function called
>> twice.  The next time will be 3 times, etc.  I think that the on handler
>> is creating one instance for the context menu and 1 for the
>> double-click, and not un-registering them when the dialog is closed.  I
>> tried on.once, but that won't work as if I happen to try the context
>> menu route and cancel it by clicking away, the double-click will never
>> get called since once will stop the events after either once fires.
>>
>> Any ideas?  I guess if I could find a way to unregister the on handler
>> when I hide the dialog, that would be the best route, but I can't find
>> out how to do that.  But I'm open to any other ideas.  I can try to put
>> together a JSFiddle if my explanation doesn't make sense.
>>
>> Thanks!
>> Harry
>>
> --
> Dojo Toolkit: http://dojotoolkit.org/
> Tutorials: http://dojotoolkit.org/documentation/
>
> [hidden email]
> To unsubscribe, visit:
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Co-Founder, Dojo Toolkit
CEO, SitePen, Inc.  http://www.sitepen.com/
Loading...