1

I have a little problem in a huge dojo-based webproject (earlier version than dojo 1.7, as far as I know it is 1.3). I want to trigger a specific event by code but don't know how I could achieve this. It's a event called ondijitclick which gets registered via the data-dojo-attach-event-attribute in html. I also can't just call the method which is triggered by this event because I only can access the domnodes (or I just don't know how I could access the objects belonging to this domnodes) at the location where I need this functionality.

I also have code with that I can at least trigger the usual events like "onclick":

        if (document.createEventObject){
            // IE
            var evt = document.createEventObject();
            node.fireEvent('onclick', evt);
        }
        else{
            // other browsers
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent('click', true, true );
            node.dispatchEvent(evt);
        }

I could enter the ondijitclick event, but nothing would happen and no errors show up. Visual Studio (with ReSharper) itself tells me that this event cannot be resolved. Because of that I think that no event gets triggered and the commands are just ignored because of unknow event type.

The whole question finally is: Exists a way to trigger such events by code for dojo 1.3 (or genereal for versions before dojo 1.7)?

Edit: clarified that a solution for dojo 1.3 is needed

Onsokumaru
  • 521
  • 1
  • 6
  • 15
  • My guess is it should be `dijitclick` without the on prefix. Since you know that there is a ondijitclick event there will be a code that attaches the event to the dijit. Can you find out how the ondijitclick has been attached to the dijit/dom node? – frank Sep 24 '14 at 13:06
  • The event is attached via the attribute in a html template file. The whole thing executes correctly when the ui-element is clicked. So it all works fine. I just want to trigger the same action by code from another place in the "application".I can't access this ui-element directly via javascript code, only using dojo.query and getting the domnode. Somehow I can't find a object which inherits the click-logic of the ui-element or gives me access to the object. – Onsokumaru Sep 24 '14 at 13:37

2 Answers2

0

Module dojo/Evented is a class that can be used as a mixin or base class, to add on() and emit() methods to a class for listening for events and emitting events.

e.g

define(["dojo/_base/declare","dojo/Evented", "dojo/Stateful"
        ], function(declare, Evented, Stateful){
                var EventedStateful = declare([Evented, Stateful], {...});
                var instance = new EventedStateful();
                instance.on("open", function(event){
                  //... do something with event
                });

            instance.emit("open", {name:"some event", ...});

In your example you have a widget that connect to ondijitclick instead of onclick (for accessibility), see dijit._OnDijitClickMixin. You can use emit() method from "dojo/on", The emit() method provides a normalized mechanism for dispatching events.

For old versions of dojo test.

        var element= dojo.byId("myElement");
        // IE does things differently
        if (dojo.isIE)
        {
            element.fireEvent("ondijitclick");
        }
        else
        { // Not IE
            var event = document.createEvent("HTMLEvents");
            event.initEvent("ondijitclick", false, true);
            console.debug(event);
            element.dispatchEvent(event);
        } 
ronielhcuervo
  • 695
  • 4
  • 10
  • Thank you for your answer, but unfortunately your solution seems to be based on dojo 1.7 (or later). The project uses an earlier version than dojo 1.7 (as mentioned), so I would need a solution for the old way of dojo... (current used version is 1.3). A upgrade is at the moment not possible. – Onsokumaru Sep 24 '14 at 12:56
0

I found a solution for how to execute the method connected to the ondijitclick-event in this case:

Since I already have the domnode, the id of the domnode is accessible. With this id I can get the whole dijit-instance with dijit.byId(node.id). When having that, it is easy to call the function of the dijit which is connected to the ondijitclick-event.

In my case I have following problems because the event-code needs some of the event-arguments and crashes when they aren't set. So I have to find out how to create and pass the event-arguments with the right property-values to the function. But this is a separate problem which is themed here

Community
  • 1
  • 1
Onsokumaru
  • 521
  • 1
  • 6
  • 15