2

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.

Ben
  • 51,770
  • 36
  • 127
  • 149
user2111743
  • 69
  • 1
  • 7

2 Answers2

4

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

Joseph
  • 5,793
  • 4
  • 34
  • 41
  • this answer was the most helpful. I spent some time hacking around trying to figure out why my perfectly formatted json wasn't actually making it to my dashboard... turns out to be an *array of hashes*. Made the change, and **poof**, it magically worked. – petemyron Feb 05 '14 at 19:19
  • I'm in this same pickle, do you by chance have sample git I can view? I have a lib/.rb file with my hashes that I wish to call in the jobs. But, aren't named in the same namaculture (label, value). Rather timestamp, top, bottom, average with values, as am building a multi-linechart – Nomad Apr 30 '16 at 22:35
0

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.

ian
  • 12,003
  • 9
  • 51
  • 107