0

I have some code build by someone else and I am trying to alter some form values when an event is dispatched

Here is the line of code

_instance.dispatchEvent ( "ADD_ROW",  { 'container' : $li, 'value' : data } );

What I can't seem to do on a global level is do something like this

someGlobalVariable.addEventListener('ADD_ROW', function (e) { console.log(11111); }, false);

or

document.querySelector('body #an_id').addEventListeneraddEventListener('ADD_ROW', function (e) { console.log(11111); }, false);

I don't know where the ADD_ROW is added or what global object I have to look at.

_instance looks like this on console.log

Object { _listenerMap: Object, indexOfListener: AbstractEventDispatcher/instance.indexOfListener(), addEventListener: AbstractEventDispatcher/instance.addEventListener(), removeEventListener: AbstractEventDispatcher/instance.removeEventListener(), hasEventListener: AbstractEventDispatcher/instance.hasEventListener(), dispatchEvent: AbstractEventDispatcher/instance.dispatchEvent(), release: AbstractEventDispatcher/instance.release(), rowCount: JSONFieldSetList/_instance.rowCount(), addRow: JSONFieldSetList/_instance.addRow(), deleteRow: JSONFieldSetList/_instance.deleteRow(), 4 more… }

Is there a way I can track what is happening and where the event is dispatched?

orbitory
  • 1,090
  • 5
  • 16
  • 40

2 Answers2

0

This _instance object, is it part of some framework? or is it just some local variable somewhere and you don't know how it was created?

Regardless, if its dispatchEvent method behaves as defined in the EventTarget interface, then the object referenced by _instance is the target. An object doesn't need to be a node, or in the DOM, to be a valid event target, it only needs to implement EventTarget. For example, XHR objects and EventSource objects can listen to DOM events, yet those events will not traverse the HTML DOM to get there.

Can you not just add your listener to _instance or keep a global reference to _instance?

Touffy
  • 6,309
  • 22
  • 28
  • I think _instance is a local/temp value. I have a global object that might contain _instance but I am not sure how to access the event. see someGlobalVariable – orbitory Mar 31 '15 at 22:07
  • OK. Do you know how to use event listeners? have you tried adding one to `_instance` directly as I suggested? if not, is there a reason you can't do it? – Touffy Apr 01 '15 at 06:44
  • that is next on my list, hopefully I can trigger one on my html element and access it using simple jQuery or a plain js selector – orbitory Apr 01 '15 at 11:53
  • I don't think so, still not sure how it was used. Maybe the event was applied on the local _instance with the hope that it will later be triggered from the main object. – orbitory Apr 02 '15 at 14:51
-2

I manage to solve it by adding my own event.

$li.trigger('ADD_ROW');

+

$('body').on('ADD_ROW', '.multi-field-set-bottom-links.row.multi-field-set-row', function(){});

Thx

orbitory
  • 1,090
  • 5
  • 16
  • 40
  • that doesn't actually answer your original question – Touffy Apr 01 '15 at 13:42
  • I know but I think the event was not accessible globally from another file. I think _instance was a private of a global object and the event is only "harvested" locally. So I created my own event(same name) on the element that I wanted to target(not some parent). This question should be deleted as it doesn't help anyone but might give ideas to others on how to solve – orbitory Apr 02 '15 at 14:55