0

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.

3
  • 1
    Every bit of javascript should be scoped to the minimum related DOM elements. I guess I should open source the in house js 'framework' I use Commented Jan 2, 2013 at 18:44
  • Of course I should scope vars in the most limited way I can. That's my question: How limited can I make them, and how? Do I need global vars? If not, what do I need then? Commented Jan 2, 2013 at 19:11
  • Basically, create a namespace: YourAppName. Then for each page you could have a namespace included in the app's one Commented Jan 2, 2013 at 19:16

2 Answers 2

3

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 :

window.claimedCount = <%= @user.claims.count %>
Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that on production your assets are precompiled, so the claims count won't update (unless you recompile your assets). .js.coffee.erb is almost never a good idea
2

If you take your code to js2coffee.com and convert it to js you will see that your variable is not global because it is inside the function.

You can check this question on how to declare global variables in coffee script (mainly you use window.myvariable )

About the second question, you could put the value in a hidden field in your html and then grab it using jquery selectors.

<input type="hidden" id="claimsCount" value=<%= @user.claims.count %>>

then in your js

$('#claimsCount').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.