Hoodie is a JavaScript library for the browser.
It offers you the following pieces of functionality right out of the box:
- user accounts and authentication
- data storage and sync
- background tasks
- sharing
- emails
- and so much more
And here is what it looks like:
// user signup
hoodie.account.signUp('[email protected]', 'secret');
// user signIn
hoodie.account.signIn('[email protected]', 'secret');
// user password change
hoodie.account.changePassword('secret', 'new_secret');
// user name change
hoodie.account.changeUsername('secret', 'newusername');
// user signout
hoodie.account.signOut();
// user password reset
hoodie.account.resetPassword('[email protected]');
// add a new document of type 'task'
hoodie.store.add('task', {
title: 'build sweetMasterApp tomorrow.'
});
// find all 'task' documents
hoodie.store.findAll('task');
// update a 'task' document
hoodie.store.update('task', '123', {
done: true
});
// remove a 'task' document
hoodie.store.remove('task', '123');
// listen to and act upon document events
hoodie.store.on('add:task', function(object) {
alert('new Task added: ' + object.task)
});
Tasks get picked up by backend workers in the background. You can think of them as special kind of objects that the describe specific tasks that you want backend logic for to be accomplished.
If a task has been completed successfully, it gets removed. If there is an error, it stays in the task store to be handled or removed.
// start a new task. Once it was finished, the succes callback gets
// called. If something went wrong, error callback gets called instead
hoodie.task.start('message', {to: 'joe', text: 'Party machen?'})
.then( showMessageSent, showMessageError )
// cancel a pending task
hoodie.task.cancel('message', '123')
// restart a pending or canceled task
hoodie.task.restart('message', '123', { extraProperty: 'value' })
// canceled all pending tasks
hoodie.task.restartAll()
// restart all pending or canceled tasks
hoodie.task.restartAll()
You can also subscribe to the following task events
- start
- cancel
- error
- success
// listen to new tasks
hoodie.task.on('start', function (newTask) {});
// task canceled
hoodie.task.on('cancel', function (canceledTask) {});
// task could not be completed
hoodie.task.on('error', function (errorMessage, task) {});
// task completed successfully
hoodie.task.on('success', function (completedTask) {});
// all listeners can be filtered by type
hoodie.task.on('start:message', function (newMessageTask, options) {});
hoodie.task.on('cancel:message', function (canceledMessageTask, options) {});
hoodie.task.on('error:message', function (errorMessage, messageTask, options) {});
hoodie.task.on('success:message', function (completedMessageTask, options) {});
hoodie.task.on('change:message', function (eventName, messageTask, options) {});
// ... and by type and id
hoodie.task.on('start:message:123', function (newMessageTask, options) {});
hoodie.task.on('cancel:message:123', function (canceledMessageTask, options) {});
hoodie.task.on('error:message:123', function (errorMessage, messageTask, options) {});
hoodie.task.on('success:message:123', function (completedMessageTask, options) {});
hoodie.task.on('change:message:123', function (eventName, messageTask, options) {});
note: if change
event is "error"
, the error message gets passed as options.error
// find all 'task' documents and publish them
hoodie.store.findAll('task').publish();
// find all documents that belong to a given user
hoodie.user( username ).findAll();
// find a given task and share it
hoodie.store.find('task', '456').share();
// find a all documents on a given share
hoodie.share(shareId).findAll();
// subscribe to a given share
hoodie.share(shareId).subscribe();
// define an email object
var magic = hoodie.email.send({
to : ['[email protected]'],
cc : ['[email protected]'],
subject : 'rule the world',
body : 'we can do it!\nSigned, Joe'
});
magic.done(function(mail) {
alert('Mail has been sent to ' + mail.to);
});
magic.fail(function(eror) {
alert('Sory, but something went wrong: ' + error.reason);
});
It's magic, stupid!™
Every app gets its own hoodie. You need to set one up, because that's whereTheMagicHappens
:
<script src="hoodie.js"></script>
<script>
whereTheMagicHappens = 'https://yourapp.hood.ie';
hoodie = new Hoodie(whereTheMagicHappens);
</script>
For more in-depth documentation, head over to hood.ie.
Have a question?
Anyone and everyone is welcome to contribute. Please take a moment to review the guidelines for contributing.
Copyright 2012, 2013 https://github.com/hoodiehq/ and other contributors
Licensed under the Apache License 2.0.