From 3f58bbd626cd84d1cb24375b4d8fe6df75ce85ba Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Sun, 4 Nov 2012 17:10:36 -0800 Subject: Adding application messaging. While widgets are the most common target for events, there is good reason to sometimes target the application as a whole. This patch adds support for such events using the /-prefixed notation. (This convention is safe for use since it represents an extremely uncommon widget ID, and one which data could not be POSTed to.) Such events are NOT subject to replay, and are handled on the client-side by firing corresponding events on the Dashing application class. As a proof of concept (and to fufill a feature request), this patch also introduces a POST /reload endpoint, which when provided with a valid authkey (and optionally a dashboard name) fires the appropriate 'reload' event on all of the loaded dashboards. The 'reload' event handler then reloads the dashboard, unless a different dashboard was specified. --- javascripts/dashing.coffee | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'javascripts') diff --git a/javascripts/dashing.coffee b/javascripts/dashing.coffee index ebf5c0a..f90c14b 100644 --- a/javascripts/dashing.coffee +++ b/javascripts/dashing.coffee @@ -25,6 +25,12 @@ Batman.Filters.shortenedNumber = (num) -> num class window.Dashing extends Batman.App + @on 'reload', (data) -> + if data.dashboard? + location.reload() if window.location.pathname is "/#{data.dashboard}" + else + location.reload() + @root -> Dashing.params = Batman.URI.paramsFromQuery(window.location.search.slice(1)); @@ -83,7 +89,6 @@ Dashing.AnimatedValue = @[k] = num @set k, to , 10 - @[k] = num Dashing.widgets = widgets = {} Dashing.lastEvents = lastEvents = {} @@ -98,9 +103,13 @@ source.addEventListener 'error', (e)-> if (e.readyState == EventSource.CLOSED) console.log("Connection closed") -source.addEventListener 'message', (e) => +source.addEventListener 'message', (e) -> data = JSON.parse(e.data) if lastEvents[data.id]?.updatedAt != data.updatedAt + # /messages are internal messages, and cannot correspond to widgets. + # We will handle them as events on the Dashing application. + return Dashing.fire(data.id.slice(1), data) if data.id[0] is '/' + if Dashing.debugMode console.log("Received data for #{data.id}", data) lastEvents[data.id] = data -- cgit v1.2.3