How can I trigger the Twitter intents events programmatically?
Asked Answered
E

1

6

The Facebook JS SDK has the equivalent of jQuery's trigger() function, FB.Event.fire that allows you to trigger the handlers you attach for particular events. Its helpful for my unit tests in QUnit. It works basically as you'd expect it to; FB.Event.fire("comment.create", location.href); fires my handlers for the comment.create event.

Twitter's object for Web Intents, twttr appears to have something that looks like it could be similar, twttr.events.trigger(), but its undocumented.

Except, I can't figure out how to trigger it correctly in code, without it throwing an error.

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

For code like:

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

I'd expect to be able to trigger it by doing something like: twttr.events.trigger("click")

Everything I try results in an error, and I'm not able to decipher the obfuscated source code.

I've put up some basic code on the JSFiddle: http://jsfiddle.net/YL6SN/

Essex answered 8/9, 2011 at 14:45 Comment(0)
A
4

Hello Yahel nice question,

It should work if you call this:

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

It expects a second parameter that is the object that is passed back to your callback. When it happens naturally this object is populated with other parameters

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

If you pass an empty object like I proposed it will work as well but in your callback you receive the object only with the type property since this is added by the trigger.

And that was exactly the error you triggered when you don't pass a second parameter. It tries to add the type parameter to undefined and and then dies.

Antediluvian answered 8/9, 2011 at 21:44 Comment(1)
Nice. Thanks, Eduardo. jsfiddle.net/YL6SN/2 works. It seems like the target is supposed to be the DOM element of the Twitter iframe (which is crucial for my testing).Essex

© 2022 - 2024 — McMap. All rights reserved.