Permalink
Browse files

Datasrouces can define Server Side Events to listen to.

Data is added to the collection of this datasource
Views are re-renderend because of collection change.

new version of W3C bootstrap, that defines the Server Side Event stream for the first datasource
  • Loading branch information...
1 parent 9fa5f87 commit 145e52e704c1eeed86ae941673f7d754a253f4d0 @steren steren committed Feb 21, 2013
Showing with 39 additions and 3 deletions.
  1. +2 −1 app/bootstrap-w3c.js
  2. +37 −2 app/sleek.js
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -185,6 +185,11 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
collection: collection,
index: index
};
+
+ // if this datasource is registered to server side event,
+ if(datasource.config.sse) {
+ self.listenToSSE(sections[index], datasource.config.sse.url);
+ }
}, self));
// Create the views once all sections have been initialized
@@ -470,7 +475,7 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
},
/**
- * Refreshes a section list.
+ * Refreshes a section list by forcing a fetch from server
*
* @function
* @param {Object} the list section
@@ -488,7 +493,7 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
},
/**
- * Updates a section list.
+ * Updates a section list (uses cache if present)
*
* @function
* @param {Object} the list section
@@ -502,6 +507,36 @@ function (Collection, DynamicContainer, Item, List, CardPanel, FadeInPanel, Fact
});
},
+ /**
+ * Make this section listen to Server Side Event (SSE)
+ *
+ * @function
+ * @param section: the section that should listen to SSE
+ * @param streamURL: utl of the SSE stream
+ */
+ listenToSSE: function(section, streamURL) {
+ var source = new EventSource(streamURL);
+
+ var self = this;
+ source.addEventListener('message', function(e) {
+ console.log(e.data);
+ var data = JSON.parse(e.data);
+
+ section.collection.add(data);
+ }, false);
+
+ source.addEventListener('open', function(e) {
+ console.log("SSE opened");
+ }, false);
+
+ source.addEventListener('error', function(e) {
+ if (e.readyState == EventSource.CLOSED) {
+ console.log("SSE lost");
+ }
+ }, false);
+
+ },
+
/**
* Displays a section list
* (assuming the section is already active).

0 comments on commit 145e52e

Please sign in to comment.