Just getting to the CoffeeScript part of my first rails app; never did any JS/CoffeeScript before, so lots of questions. Starting with:
In my app, users can “claim” (take ownership of) tasks (or ideas, as I call them). Each user may have up to 3 ideas at any given time. So:
1) My current code looks like this:
$ ->
exports = this
exports.claimedCount = 0
$('.claim').bind 'ajax:success', ->
$(this).text("Claimed")
$(this).addClass("btn-success")
claimedCount++
alert(claimedCount)
What this is supposed to do is declare a global var claimedCount and increment it every time the user claims an idea. What is actually does is nothing: Uncaught ReferenceError: claimedCount is not defined
Why is that? How should I really define the variable? Do I even need a global variable for what I’m trying to do? (Each idea has its own Claim button).
2) The user might render the page when they already have a few ideas claimed. Thus, I need to set the claimedCount using Rails when first rendering the page. Any pointers on this would be greatly appreciated (should I rename my .js.coffee file to .js.coffee.erb and do it there? What’s the usual spot for js init code like this?)
Many thanks for any help you can give.
I agree with Khaled’s answer on the first question.
On the second one, I would rather rename the file to .js.coffee.erb so that it is preprocessed with erb first and write something like :