I'm trying to get a function into the asset pipeline in coffeescript
If I use pure javascript,
mymodel.js
function restrictPlayback(event) {
// Trying to stop the player if it goes above 1 second
if (event.currentTime > 10) {
event.pause();
event.currentTime = 0
}
}
This compiles fine and the function works.
If I put the following:
mymodel.js.coffee
restrictPlayback = (event) ->
# Trying to stop the player if it goes above 10 seconds
if event.currentTime > 10
event.pause()
event.currentTime = 0
I get the following error
Uncaught ReferenceError: restrictPlayback is not defined
What am I doing wrong?
I suspect that you're defining the function and then expecting it to have global scope (ie usable from anywhere).
By default, Coffeescript wraps compiled code into Immediately Invoked Function Expressions (IIFEs) so the function you declared is only valid within the scope of the Coffeescript file.
You can get Coffeescript to stop wrapping in IIFEs by using the -b flag when compiling although it's much better practice to learn to do things the Coffeescript way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.