user1445685
user1445685

Reputation: 883

Promotion/bubble-wrap TableScreen data

I'm stuck on this:

I need to populate data into my app.

I'm using Promotion for the very first time....

Without ProMotion I use to fetch the data in the init method

Now my code looks like below:

class Parties < ProMotion::TableScreen
  attr_accessor :_cells
  @news = []
  include MyUiModules
  title 'Yazarlar'
  refreshable callback: :on_refresh,
     pull_message: "Pull to refresh",
     refreshing: "Refreshing data…",
     updated_format: "Last updated at %s",
     updated_time_format: "%l:%M %p"
 def on_refresh
   #MyItems.pull_from_server do |items|
   #@my_items = items
   end_refreshing
   #update_table_data
   # end
 end

 def table_data
   _cells = []
   [{
     title: nil,
     cells: create_cells(_cells)
   }]            
 end
 def will_appear
    Barbutton.create_bar(self)
    set_attributes self.view, {
      backgroundColor: hex_color("DBDBDB")
    }        
  end

  def go_to_next
    App.delegate.slide_menu.show_menu    
  end

  def create_cells(_cells)

   BW::HTTP.get(URL) do |response|
     json = BW::JSON.parse response.body.to_str
     for line in json
       _cells << { title: line["val_for_title"]}
     end
   end
   _cells
  end
end  

Unfotunately this does return an empty array, and I can't figure out how to solve it. Thx for your help

Upvotes: 0

Views: 207

Answers (1)

Arkan
Arkan

Reputation: 6236

You can't do that because BW::HTTP.get is asynchronous !

Instead try something like this:

def on_init
  @data = []
end

def table_data
  [
    {
      title: nil,
       cells: @data
     }
  ]            
end

def on_refresh
  BW::HTTP.get(URL) do |response|
    @data = []
    json = BW::JSON.parse(response.body.to_str)
    json.each do |hash|
      @data << { title: hash["val_for_title"]}
    end
    update_table_data
    end_refreshing
  end
end

Hope it helps :-)

Upvotes: 3

Related Questions