Skip to content

Instantly share code, notes, and snippets.

@gr2m
Last active December 17, 2015 00:19
Show Gist options
  • Save gr2m/5520395 to your computer and use it in GitHub Desktop.
Save gr2m/5520395 to your computer and use it in GitHub Desktop.
Hoodie empowers frontend developers to build fully capable apps, without thinking backend. We've put a lot of thoughts into its API and made a proof-of-concept implementation, but now want to reach out to gather opinions on how hoodie.js should be implemented "the proper JavaScript way".

hoodie.js is a library to abstract common backend tasks in form of frontend developer friendly JavaScript methods.

The API is what we care most about. No matter of how complex are behind the curtain, hoodie.js has to remain as simple as jQuery.

The current implementation is done with CoffeeScript, for reasons. But we want to move to make a proper JavaScript implementation and want to invite you to help us:

  • what's your opinion on how the hoodie.js modules should be structured, the "JavaScript way"?
  • what are libraries we should look into / learn from?
  • this is your chance: do you want to help migrating hoodie.js to JavaScript? Raise your hand
//
// INITIALIZE HOODIE
//
// The idea is, that one can use several Hoodie instances on the same page,
// in order to interact with the different backends.
//
whereTheMagicHappens = 'https://yourapp.hood.ie'
hoodie = new Hoodie(whereTheMagicHappens)
//
// ACCOUNT MODULE
//
// the hoodie.account module provides methods for common account related
// tasks, like signing Up for a new account, resetting a password or changing
// a username.
//
// All methods are asynchronous and return a promise.
//
hoodie.account.signUp('joe@example.com', 'secret')
hoodie.account.signIn('joe@example.com', 'secret')
hoodie.account.signOut()
hoodie.account.changePassword('currentpassword', 'newpassword')
hoodie.account.changeUsername('currentpassword', 'newusername')
hoodie.account.resetPassword('joe@example.com')
hoodie.account.destroy('currentpassword')
//
// STORE MODULE
//
// the hoodie.store module provides methods to store and retrieve JSON objects.
// It gets automatically synchronized when the user is signed in, so data stored
// on one device is accessible on all devices by default.
//
// All methods are asynchronous and return a promise.
//
// store a new task
type = 'task'
attributes = {title: "remember the milk"}
hoodie.store.add(type, attributes ).done ( function(newObject) { } )
// update an existing task
type = 'task'
id = 'abc4567'
update = {size: 2}
hoodie.store.update( type, id, update ).done ( function(updatedObject) { } )
// find a specific task
type = 'task'
id = 'abc4567'
hoodie.store.find( type, id ).done ( function(object) { } )
// Load all tasks
type = 'task'
hoodie.store.findAll( type ).done ( function(objects) { } )
// remove an existing task
type = 'task'
id = 'abc4567'
hoodie.store.remove( type, id ).done ( function(removedObject) { } )
//
// REMOTE MODULE
//
// Hoodie.Remote provides methods to interact with data that
// is stored remotely and therefore need HTTP requests.
//
// To instantiate Hoodie.Remote, use hoodie.open( name ).
// hoodie.remote is a special Hoodie.Remote instance that is already
// connected to the user's remote database.
//
// Most methods are asynchronous and return a promise.
//
var remote = hoodie.open("public_charts")
// find all objects of type = "track"
remote.findAll('track').then( function(tracks) { /* ... */ })
remote.add("track", { title: "Hoodie Hoodie Hoodie!" })
// listen to changes on remote and react on newly added tracks
remote.connect()
remote.on("add:track", playNewTrack )
//
// EXTENDING HOODIE
//
// Extending hoodie should be easy, whatever that means ;-)
// hoodie.js has a syntax to enxtend it, but it's not widely used yet and
// can be changed if we come up with a better syntax.
//
// Current extensions are: hoodie.user, hoodie.share, hoodie.email.
// These live in the hoodie.js core repository, but we may move
// these into own repositories.
//
// write hoodie extensions
function SayHi( hoodie ) {}
SayHi.prototype.sayHi = function() {
alert("hi!")
};
Hoodie.extend('sayHi', SayHi)
// use it
hoodie = new Hoodie()
hoodie.sayHi() // alerts 'hi!'
//
// USER / PUBLIC STORE EXTENSION
//
// hoodie.user( username ) returns a Hoodie.Remote instance, connected
// to the public store of the passed user, which is read only.
//
// load all favorites by joe@example.com
hoodie.user( 'joe@example.com' ).findAll( 'favorite' )
// publish all my favorits, so other users can access them
hoodie.store.findAll('favorite').publish()
// unpublish old favorites
selectOldFavorites = function(object) {
return object.type === 'favorite' && object.createdAt < "2013"
};
hoodie.store.findAll( selectOldFavorites ).unpublish()
//
// SHARE EXTENSION
//
// the share extension allows to collaborate on specific objects
// with other users.
//
// load or update objects in a share
hoodie.share( 'secret123xyz' ).findAll( 'task' )
hoodie.share( 'secret123xyz' ).add( 'task', {title: "make the sun shine"} )
// share my tasks with Joe & Jane
var myShare;
hoodie.store.findAll('task').share( function(share) { myShare = share })
myShare.grantWriteAccess(['jane@xm.pl', 'joe@xm.pl'])
myShare.grantReadAccess(['boss@xm.pl'])
alert( "tasks are shared at " + myShare.id)
// also share the comments at the same share
hoodie.store.findAll('comment').shareAt( myShare )
// stop sharing
myShare.destroy()
hoodie.share.remove( '123' ) // same result given myShare.id is '123'
@Acconut
Copy link

Acconut commented May 5, 2013

For the module system I wouldn't use an extra library. We could use this tiny extend script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment