English | 中文 | Русский | Français
2,856 Posts served
8,606 Conversations started
You are a JavaScript programmer. You want to write an application that can access some really cool sensors on the client platform: accelerometer, gps, thermometer, compass, ambient light sensor, etc. There are different ways that such an API might show up. For example, to access temperature on the client, you might do this:
var t = device.thermometer.temperature;
or, it might look like this:
var t = device.get('thermometer', temperature);
If the accelerometer event and error handlers looked like this:
function handleMovement( accleration ) { //do stuff }function handleError( accleration ) { //do stuff }
subscribing to events could either be
var watchId = device.movement.watchMovement( handleMovement, handleError, options);
or
var watchId = device.watch( 'movement', handleMovement, handleError, options );
In other words, You can either have a concrete API that has specific JavaScript syntax for each sensor's properties and events, or you can have a generic, dispatch-like API that is tied to a very specific ontology, vocabulary or list of acceptable values that can be used as parameters. Considering the vast range of sensors that might be included in the foreseeable future, the size of the API could be quite large and complex; alternatively, the API could be simple and the ontology could be complex.
The W3C Geolocation API uses the former; the W3C DCCI API and BONDI use the latter (here are the links to the DCCI and BONDI vocabularies).
All other advantages and disadvantages aside, which API style do you prefer?
| November 1, 2009 5:18 AM PST
antiquus
|
I don't believe I just did that... :D The example should be... var t = device.get('thermometer',temperature); var watchId = t[0].watchMovement( etc. ); |
| November 2, 2009 12:27 PM PST
Clayne Robison (Intel)
|
I agree that flexibility is major benefit the dispatch-like API, as would be the simplicity of learning (and remembering) it. I also agree that attaching watch to the property to be watched is more logical and object oriented than passing in the name of the property being watched. Making watch() a single arg function could be challenging unless you want to make that argument an object/array. The main arg is the function to call when something's happened; the second arg would be the function to call when something wen't wrong, and the third arg could be a way to filter events by sample interval or value. But frankly, the filtering could take place inside the receiving function. |

antiquus
10
Registered User
The device.watch() function should be single-valued, though, and take a single-valued argument. In other words, device.watch() should be a property of the device itself (poor choice of identifier here).
var t = device.accelerometer[0].acceleration; // assuming the first is the correct choice
var watchId = t.watchMovement( etc. );