user2111743
user2111743

Reputation: 69

Dashing Dashboard Framework Passing Label & Value to a List Widget

To pass data into the view, the generic "job" is set up as so:

SCHEDULER.every '1m', :first_in => 0 do |job|
  send_event('widget_id', { })
end

In the view, it is processed as such:

<li data-foreach-item="items">
  <span class="label" data-bind="item.label"></span>
  <span class="value" data-bind="item.value"></span>
</li>

I am not sure how to pass in a hash (or more broadly, collection) that is able to be read in that label, value format. If someone could point me in the right direction I sure would appreciate it. I can't find any helpful documentation.

Upvotes: 2

Views: 4500

Answers (2)

Joseph
Joseph

Reputation: 6031

The documentation is quite deceiving though, you pass in an array with a Hash

Here is what i did to use a List

buzz  = [{:label=>"Count", :value=>10}, { :label=>"Sort", :value=>30}]
 send_event('buzzwords', { items: buzz })

The above works, but if i do the following :

buzz  = [{:label=>"Count", :value=>10}, { :label=>"Sort", :value=>30}]
items = buzz.to_json
send_event('buzzwords', { items: items})

That does not work, but the documentation says send_event(widget_id, json_formatted_data) The item is json formatted but that does not work, instead pass in an array with a Hash

Upvotes: 4

ian
ian

Reputation: 12251

Disclaimer: I've not used Dashing (although it looks quite interesting).

From the docs:


send_event('karma', { current: rand(1000) })

This job will run every minute, and will send a random number to ALL widgets that have data-id set to 'karma'.

You send data using the following method:

send_event(widget_id, json_formatted_data)

So for your collection, you need an array of hashes, each hash has the keys label and value (as instance method calls on an object in coffeescript are (in Ruby speak) really just accessors on a hash).

Once you have that collection, transform it into JSON, and stick it in an object with the accessor items, e.g.

require 'json'
items = [{label: "l1", value: "v1"},{label: "l2", value: "v2"},{label: "l3", value: "v3"}]
json_formatted_items = items.to_json
# => "[{\"label\":\"l1\",\"value\":\"v1\"},{\"label\":\"l2\",\"value\":\"v2\"},{\"label\":\"l3\",\"value\":\"v3\"}]"

SCHEDULER.every '1m', :first_in => 0 do |job|
  send_event('widget_id', {items: json_formatted_items })
end

I don't know if that will work, but that's what I think will work. Hope it helps.

Upvotes: 0

Related Questions