What does it all hang off of?

Hi all,

one issue we have with APIs is also how they are exposed. Say for  
instance that the Contacts API has a basic interface providing access  
to the rest of the functionality called "Contacts". We could:

1) Make it available as a constructor:

   var cs = new Contacts();
   var mum = cs.findOne({ nickname: "mum" });

This pollutes the global namespace (since "Contacts" is now a variable  
in that scope), and is useless since there's no use for multiple  
instantiation of that class.

2) Make it available as an object already instantiated in the global  
scope:

   var mum = contacts.findOne({ nickname: "mum" });

This is nicer, less useless, but still polluting. Given the number of  
APIs that we have, it's probably a bad idea.

3) Make a way of requesting that an API be "loaded":

   var mum = loadAPI("contacts").findOne({ nickname: "mum" });

This adds only one item to the global scope (which is acceptable), but  
isn't idiomatic.

4) Use an object to provide access to all the device APIs:

   var mum = device.contacts.findOne({ nickname: "mum" });

I think the latter is the cleanest option. Specifying one such  
extensible object is trivial, and would be a very short specification.

Another option is to hang off of the navigator object just like  
geolocation did — but that seems wrong (I'm not sure why it was chosen  
there).

Thoughts?

-- 
Robin Berjon - http://berjon.com/

Received on Friday, 2 October 2009 12:33:11 UTC