Search code examples
voltvoltrb

Task not setting variables properly?


Having some issues getting a variable passed to the client. The task that gets run is this:

def getmaps
  $mapnames_current = []
  url = 'http://s3-ap-northeast-1.amazonaws.com/splatoon-data.nintendo.net/stages_info.json'
  resp = Net::HTTP.get_response(URI.parse(url))
  buffer = resp.body
  result = JSON.parse(buffer)
  result.each do |gamemode|
    gamemode['stages'].each do |stage|
      $mapnames_current << $mapnames.key(stage['id'])
    end
  end
end

and $mapnames_current gets called here:

    <div class="text-center">
      <h2>Current Maps!</h2>
      <h3>Turf War:</h3>
      <h2>{{ $mapnames_current[0] }}</h2>
      <h2>{{ $mapnames_current[1] }}</h2>
      <h3>Ranked:</h3>
      <h2>{{ $mapnames_current[2] }}</h2>
      <h2>{{ $mapnames_current[3] }}</h2>
    </div>

I'm not sure what's going wrong here. Shouldn't the $mapnames_current variable be accessible everywhere?


Solution

  • Global variables aren't synced between client and server. The only thing passed between the client and server from a task is the return values of a task. (Which should be something that can be serialized to json (or date types in json)).

    There's a good screencast on tasks here: http://datamelon.io/blog/2015/creating-volt-task-objects.html