Ruby on rails Dashing rails:如何更改小部件';s背景色以反映布尔数据值?

Ruby on rails Dashing rails:如何更改小部件';s背景色以反映布尔数据值?,ruby-on-rails,dashing,Ruby On Rails,Dashing,我正在使用并尝试根据从API返回的布尔值将小部件的背景颜色更改为绿色/红色。我的工作是: Dashing.scheduler.every '1m', first_in: 8 do Dashing.send_event('all_good', status: MixpanelHelper.all_good?) end 其咖啡脚本包括以下内容的小部件: onData: (data) -> if data.status $(@node).css('background-colo

我正在使用并尝试根据从API返回的布尔值将小部件的背景颜色更改为绿色/红色。我的工作是:

Dashing.scheduler.every '1m', first_in: 8 do
  Dashing.send_event('all_good', status: MixpanelHelper.all_good?)
end
其咖啡脚本包括以下内容的小部件:

onData: (data) ->
  if data.status
    $(@node).css('background-color', '#42b2aa')
  else
    $(@node).css('background-color', '#e85c28')
    <div data-id="all_good" data-view="Mywidget" data-title="All good" data-goal="95%" data-suffix="%"></div>
以及仪表板,其erb包括:

onData: (data) ->
  if data.status
    $(@node).css('background-color', '#42b2aa')
  else
    $(@node).css('background-color', '#e85c28')
    <div data-id="all_good" data-view="Mywidget" data-title="All good" data-goal="95%" data-suffix="%"></div>


但是颜色变化并没有被触发。我没有正确地传递数据吗?

几天前,我刚开始使用dash,我也遇到了同样的问题。我解决这个问题的方法是把它放在两个地方,两个都是ready:和onData:类似这样的事件

  ready: ->
    # This is fired when the widget is done being rendered
    @setColor(@get('status'))

  onData: (data) ->
    # Handle incoming data
    # You can access the html node of this widget with `@node`
    # Example: $(@node).fadeOut().fadeIn() will make the node flash each time data comes in.
    @setColor(@get('status'))
    $(@node).fadeOut().fadeIn()

  setColor: (status) ->
    if status
      switch status
          when 'RUN' then $(@node).css("background-color", "#29a334") #green
          when 'FAIL' then $(@node).css("background-color", "#b80028") #red
          when 'PEND' then $(@node).css("background-color", "#ec663c") #orange
          when 'HOLD' then $(@node).css("background-color", "#4096ee") #blue

我认为这不是最好的解决方案,可能还有另一个可以使用的事件,一个我还不知道的事件,如果我发现它,将更新此答案,但在此期间,您可能想使用它。

sammydc的答案是正确的。。在创建一个新的小部件(name=XZryRk)时,根据我的经验(今天我第一次使用Dash)添加了一些信息:

运行此命令以创建新的小部件:

破折号生成小部件XZryRk

输出:

widgets/xzryryrk/xzryryrk.html
widgets/xzryryrk/xzryryrk.scss
widgets/xzryryrk/xzryryrk.coffee
  • 编辑“xzryrk.coffee”以添加sammydc在上述帖子中提到的代码,但请记住空格和标签。虚线是基于对空间和制表符敏感的语言

  • 编辑“XZryRk.scss”:默认情况下,新小部件没有任何css配置,您需要添加以下css(根据您的要求编辑名称):

  • 编辑“xzryrk.html”:默认情况下,它也是空的。添加以下内容:

    
    

  • 最后,在(可能是/dashing/dashboards/sample.erb)中编辑erb文件

    `curl -d '{ "auth_token": "YOUR_AUTH_TOKEN", "title":"XXX" ,"status": "FAIL" ,"text":"FAIL"}' http://X.X.X.X:8082/widgets/xzryryrk`