Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:54:20+00:00 2026-06-05T12:54:20+00:00

I am working on rails3.2 and using coffeescript In my coffeescript i have a

  • 0

I am working on rails3.2 and using coffeescript

In my coffeescript i have a for loop and for each , i am executing a API call to fetch results and appending the results to the respective dom

like

for ajaxLink in _ajaxRenderLinks
      _template = $(ajaxLink).attr('data-link-template')
      apiURL = $(ajaxLink).attr('data-link-url')
      console.log($(ajaxLink))
      Myapp.API.execute(apiURL, 'get', {}, (data) ->
        console.log($(ajaxLink))
        if data isnt "" or data isnt undefined
          console.log($(ajaxLink))
          $(ajaxLink).empty()
          $(ajaxLink).append($(HandlebarsTemplates[Myapp.TemplateRoutes.path(_template)](data))))

In the above code, i have two links (one is a link and another is a ul link)
ajaxRenderLinks has these a and ul which has its template to render and the url to fetch the results.
The first console.log prints the first link a and then runs the API call for it and then runs the APi call for it
and then it prints the second link ul and runs the API CALl for it . And finally it tries to append the last result with the second link and not doing the appending for each link, since it ajaxLink inside if data isnt “” or data isnt undefined prints only the second link and not prints the first link and then second link

How to resolve this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:54:21+00:00Added an answer on June 5, 2026 at 12:54 pm

    Looks like a classic “variable closure in a loop” situation. Because the execute function is asynchronous, it references ajaxLink, which changes as the loop loops. So when execute‘s callback is invoked, ajaxLink might be something else than what it was when execute itself was invoked.

    Try this

    for ajaxLink in _ajaxRenderLinks
      do (ajaxLink) ->         # wrap in an immediately-invoked function
        ajaxLink = $(ajaxLink) # save this rather than calling $(...) again and again
        _template = ajaxLink.attr('data-link-template')
        apiURL = ajaxLink.attr('data-link-url')
        Myapp.API.execute(apiURL, 'get', {}, (data) ->
          if data? and data isnt ""
            ajaxLink.empty().append($(HandlebarsTemplates[Myapp.TemplateRoutes.path(_template)](data))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on rails 3.2 and using coffeescript too.. I have a doubt
I have a working Rails application on version 2.3.5 - I am using many
I am using capybara and cucumber in my rails3 application. Everything is working just
I have jquery working on my Rails app using the gem 'jquery-rails' . I
I am working on a web page that is using jQuery. I have an
I am using Rails 3, Jquery-UJS and Jquery. I have a working setup of
I'm working on a Rails app using CanCan for RBAC and I only have
I am working on rails 3 app and using the koala gem to get
I'm using Rails 3.2, Carrierwave and simple_form. Currently, everything is working fine with one
I am working on rails 3 and sqlite db. Using a IN query. Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.