I think I found a good solution for getting a callback to work in the swfObject library that is very similar to txominpelu's suggestion. Seems like using -> callbackFn
works nicely, basically creating an anonymous function and returning the callback? I'd appreciate anyone setting me straight on why that might be all wrong. (I'm usually wrong the first time around)
I'm using swfobject.embedSWF
with a callbackFn
that works fine in my hand coded JavaScript file where I use function callbackFn() {}
to define it. Now I'm trying to move over to CoffeeScript, which is so nice. So CoffeeScript will compile callbackFn = function() {};
instead. My original swfObject.embedSWF doesn't work with the CoffeeScript compiled version of my callback.
// original javascript file approach
swfobject.embedSWF( "swf/flashContent.swf", "site-content",
"100%", "100%", "10.0.0", false,
flashvars, params, attributes, callbackFn );
// original javascript callback
function callbackFn() {
log( "--> callbackFn invoked ");
// ... do stuff
}
I'm new to CoffeeScript and I think the only time you get named functions is with a class
I started by trying an anonymous function in place of the optional callback parameter that's detailed on the swfObject wiki. Then I figured I might be able to get something like that to work with the CoffeeScript version. Here's where I ended up.
# part of .coffee file
swfobject.embedSWF "swf/flashContent.swf", "site-content",
"100%", "100%", "10.0.0", false,
flashvars, params, attributes, -> callbackFn()
callbackFn = (evt) ->
console.log "--> callbackFn invoked #{evt}"
# do other stuff
# part compiled .js file
swfobject.embedSWF("swf/flashContent.swf", "site-content",
"100%", "100%", "10.0.0.", false,
flashvars, params, attributes, function() {
return callbackFn();
});
callbackFn = function() { ... }
I also looked at passing the event through the callbackFn too and that looks like it works.
# .coffee file swf
@swfobject.embedSWF "swf/flashContent.swf", "site-content",
"100%", "100%", "10.0.0.", false,
flashvars, params, attributes, -> callbackFn(@swfobject)
callbackFn = (evt) ->
console.log "--> callbackFn invoked #{evt}"
# do other stuff