Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript async queries: should I put callback in each method or in an object?
    primarykey
    data
    text
    <p>I am working on a JavaScript framework - front-end for <a href="http://databrewery.org/cubes.html" rel="nofollow">Cubes</a> for the <a href="http://packages.python.org/cubes/server.html" rel="nofollow">Slicer OLAP server</a>. I am now about to implement aggregation browser object, which will perform queries to the server. Proposed API example:</p> <ul> <li>browser.aggregate(cell, {drilldown:some_dimension}) - get aggregation of a cell and drill-down through a dimension</li> <li>browser.facts(cell) - get all facts within a cell</li> <li>...</li> </ul> <p>I am thinking about three options of handling the result:</p> <p><strong>A:</strong> in each browser call:</p> <pre><code>browser.aggregate(cell, options, handler) </code></pre> <p>or <strong>B:</strong> in the browser for any query:</p> <pre><code>browser.result_handler = my_result_handler browser.aggregate(cell, options) </code></pre> <p>Where the result handler would be something like:</p> <pre><code>result_handler = function(browser, result, context) { ... } </code></pre> <p>or <strong>C:</strong> delegate object:</p> <pre><code>browser.delegate = my_controller </code></pre> <p>Where the my_controller would have methods that will correspond to the browser query methods.</p> <p>or is there any another option <strong>D</strong>?</p> <p>I am very new to JavaScript (in fact, this is my first thing written in the language). What is the most common way of doing this kind of async handling in JS frameworks? I am more inclined to the common handler in the browser, option <strong>B</strong>.</p> <p>The framework I am creating is here: <a href="https://github.com/Stiivi/cubes.js" rel="nofollow">https://github.com/Stiivi/cubes.js</a> (using underscore.js and jQuery)</p> <p>I have a working example if someone is interested, I am not putting it here, as it is not very polished, just sand-box/playground kind of thing.</p> <p>I would be very grateful for any constructive tips. </p> <p><strong>[EDIT]</strong> Note that the browser object is not one-time disposable object for one query, it is reused frequently for each part of a report in a web application. It is used, as name suggests, for browsing the data. Want a chart? Use browser for aggregation. Want a table? Use browser. Want to drill-down/get more details? Use the same browser. "Cell" object holds current browsing context, it is like multi-dimensional "current directory" if compared to single-dimensional file system.</p> <p><strong>[EDIR 2]</strong> Example of usage: you have a page with a bar chart, aggregated by year, and a pie chart with table aggregated by a category. The pie+table contains same data. You will need two requests for two contexts: <code>bar</code> and <code>pie</code>:</p> <pre><code>browser.aggregate(cell, { context: "bar" }, handler) browser.aggregate(cell, { context: "pie" }, handler) </code></pre> <p>or when handler is in the browser:</p> <pre><code>browser.aggregate(cell, { context: "bar" }) browser.aggregate(cell, { context: "pie" }) </code></pre> <p>handler in the browser removes some code noise.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload