How can I programmatically trigger Twitter events?

The JS SDK for Facebook has the equivalent of the jQuery trigger() function, FB.Event.fire which allows you to run handlers that you attach for certain events. This is useful for my unit tests in QUnit. It works basically the way you expected; FB.Event.fire("comment.create", location.href); fires my handlers for the comment.create event.

The tweet object for web intents, twttr , seems to have something similar that it may be similar, twttr.events.trigger() , but it is not documented.

In addition, I can’t figure out how to run it correctly in the code without causing an error.

How can I programmatically check the handlers that I attach to this object?

For code like:

 twttr.events.bind("click", function(intent){ console.dir(intent); }); 

I expect I can call it by doing something like: twttr.events.trigger("click")

Everything I try to get results in an error and I cannot decrypt the obfuscated source code .

I installed the base code in JSFiddle: http://jsfiddle.net/YL6SN/

+6
source share
1 answer

Hi, Yahel a good question,

It should work if you call this:

 twttr.events.trigger("click", {}); 

It expects a second parameter, which is an object that returns back to your callback. When this happens naturally, this object is filled with other parameters

 {target: b,region: "intent",type: "click",data: {}} 

If you pass an empty object, as I suggested, it will work, but in your callback you only get the object with the type property, as this is added by the trigger.

And that was exactly the error you caused when you did not pass the second parameter. It tries to add the type parameter to undefined and then dies.

+3
source

Source: https://habr.com/ru/post/896887/


All Articles