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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With