Skip to content

Instantly share code, notes, and snippets.

@baumandm
Last active June 27, 2016 14:59
Show Gist options
  • Select an option

  • Save baumandm/7f339a4064ef59b17c9fba5da9bc073b to your computer and use it in GitHub Desktop.

Select an option

Save baumandm/7f339a4064ef59b17c9fba5da9bc073b to your computer and use it in GitHub Desktop.

Revisions

  1. baumandm revised this gist Jun 27, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cyclotron-javascript-datasource-merge.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Cyclotron: JavaScript Data Source to execute and merge two Data Sources together

    This is a JavaScript Data Source which executes two other data sources, and merges their results together based on some key. Since Widgets can only use a single Data Source at once, this is the solution for combining Data Sources.

    Note: both datasource0 and datasource1 need to have "preload": true and "deferred": true, so that .execute() can be called.
  2. baumandm revised this gist Jun 27, 2016. 1 changed file with 32 additions and 32 deletions.
    64 changes: 32 additions & 32 deletions cyclotron-javascript-datasource-merge.md
    Original file line number Diff line number Diff line change
    @@ -3,36 +3,36 @@ This is a JavaScript Data Source which executes two other data sources, and merg
    Note: both datasource0 and datasource1 need to have "preload": true and "deferred": true, so that .execute() can be called.

    Processor:

    p = function (promise) {

    p = function (promise) {

    /* Execute two data sources simultaneously */
    var p1 = Cyclotron.dataSources['datasource0'].execute(true);
    var p2 = Cyclotron.dataSources['datasource1'].execute(true);
    /* Wait for the first data source to complete */
    p1.then(function (result) {
    var r1 = result['0'].data;

    /* Wait for the second data source to complete */
    p2.then(function (result) {
    var r2 = result['0'].data;
    /* Join on shared key */
    var merged = _.map(r1, function(row1) {
    var row2 = _.find(r2, { key: row1.key });
    return _.merge(_.clone(row1), row2);
    });
    /* Resolve promise with merged row */
    promise.resolve(merged);
    });
    });

    /* Catch errors and reject promise, so errors appear in the Widget */
    p1.catch(function (err) { promise.reject(err); });
    p2.catch(function (err) { promise.reject(err); });

    /* Return nothing, so Cyclotron will wait for promise to resolve */
    }
    /* Execute two data sources simultaneously */
    var p1 = Cyclotron.dataSources['datasource0'].execute(true);
    var p2 = Cyclotron.dataSources['datasource1'].execute(true);
    /* Wait for the first data source to complete */
    p1.then(function (result) {
    var r1 = result['0'].data;

    /* Wait for the second data source to complete */
    p2.then(function (result) {
    var r2 = result['0'].data;
    /* Join on shared key */
    var merged = _.map(r1, function(row1) {
    var row2 = _.find(r2, { key: row1.key });
    return _.merge(_.clone(row1), row2);
    });
    /* Resolve promise with merged row */
    promise.resolve(merged);
    });
    });

    /* Catch errors and reject promise, so errors appear in the Widget */
    p1.catch(function (err) { promise.reject(err); });
    p2.catch(function (err) { promise.reject(err); });

    /* Return nothing, so Cyclotron will wait for promise to resolve */
    }
  3. baumandm renamed this gist Jun 27, 2016. 1 changed file with 0 additions and 0 deletions.
  4. baumandm created this gist Jun 27, 2016.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    This is a JavaScript Data Source which executes two other data sources, and merges their results together based on some key. Since Widgets can only use a single Data Source at once, this is the solution for combining Data Sources.

    Note: both datasource0 and datasource1 need to have "preload": true and "deferred": true, so that .execute() can be called.

    Processor:

    p = function (promise) {

    /* Execute two data sources simultaneously */
    var p1 = Cyclotron.dataSources['datasource0'].execute(true);
    var p2 = Cyclotron.dataSources['datasource1'].execute(true);

    /* Wait for the first data source to complete */
    p1.then(function (result) {
    var r1 = result['0'].data;

    /* Wait for the second data source to complete */
    p2.then(function (result) {
    var r2 = result['0'].data;

    /* Join on shared key */
    var merged = _.map(r1, function(row1) {
    var row2 = _.find(r2, { key: row1.key });

    return _.merge(_.clone(row1), row2);
    });

    /* Resolve promise with merged row */
    promise.resolve(merged);
    });
    });

    /* Catch errors and reject promise, so errors appear in the Widget */
    p1.catch(function (err) { promise.reject(err); });
    p2.catch(function (err) { promise.reject(err); });

    /* Return nothing, so Cyclotron will wait for promise to resolve */
    }