Skip to content

Instantly share code, notes, and snippets.

@gr2m
Created October 27, 2012 19:26
Show Gist options
  • Save gr2m/3965763 to your computer and use it in GitHub Desktop.
Save gr2m/3965763 to your computer and use it in GitHub Desktop.
JavaScript Dream Code: share.js

Imagine there would be a JavaScript library called share.js. It let's you share data with others by secret keys. How would the JavaScript code look like for such a module?

Example: you have a todo list:

var todoList = { title: "shopping list" }
var todo1 =    { title: "milk", done: false}
var todo2 =    { title: "cornflakes", done: false}
var todo3 =    { title: "banana", done: false}

Now I want to send the list to my spouse so we can work on it together. How would a JavaScript dream API look like to do that?

I'll start with my own dream code — what's yours?

// 1. I share my todoList with its todos
// And email with a Link containing a secret share key will
// be sent to spouse@example.com
var share = new Share()
share.insert('todolist', todoList)
share.insert('todo', [todo1, todo2, todo3])
share.invite('spouse@example.com' )
share.publish()
.fail( function(error) { /* handle error */ } )
.done( function() {} )
// 2. My spouse klicks the link 'http://myapp.com/invite/wyfl290',
// then the following code is used to access the shared data
var share = Share.open('wyfl290')
share.findAll()
.fail( function(error) { /* handle error */ } )
.done( function( objects ) {
// objects is an array containing the todolist with its todos
} )
//
// Entire Share Module API
//
// initiate a new share
share = new Share
// open an existing share
share = Share.open('secretkey')
// inserting / finding / updating / removing objects
share.find( objectKey )
share.findAll( filter ) // filter can be typo or function
share.update( objectKey, update ) // update can be hash or function
share.remove( objectKey )
// push share
share.publish()
// destroy share
share.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment