Skip to content

Instantly share code, notes, and snippets.

@artursapek
Created June 14, 2012 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artursapek/2933472 to your computer and use it in GitHub Desktop.
Save artursapek/2933472 to your computer and use it in GitHub Desktop.
todo.js
cm.destroy(key)
.on('complete', function(){
$('span[item="' + key + '"]').remove();
})
// Passing the null parameter gets all data. Passing in a specific key would get a specific object.
cm.get(null).on('success', function(data){
// Save the data locally
local_data = data;
// Change the UI to draw each item in the list
for (var i in data){
draw_new_item(data[i].text, data[i].priority, data[i].deadline);
}
});
// These values can be found in the CloudMine dashboard
var init_vals = {
appid: '84e5c4a381e7424b8df62e055f0b69db',
apikey: '84c8c3f1223b4710b180d181cd6fb1df'
}
var cm = new cloudmine.WebService(init_vals);
var credentials = {
userid: 'artur@cloudmine.me',
password: 'basedgod'
};
cm.login(credentials)
.on('success', function(data){
// If we log in successfully, save the session_token and show the list!
process_login(data);
})
.on('unauthorized', function(data){
// If the password and/or email are incorrect, reset the UI
// and show the error message from the server
$('#login_button').attr('value', 'Login');
$('#register_button, #or').show();
show_error('login', data.errors[0]);
});
// While the createUser request is pending, change the UI to indicate there's activity.
$('#register_button').attr('value', 'Creating account...');
$('#login_button, #or').hide();
// Get registration data from the UI
var userid = $('#email_field').val(),
password = $('#password_field');
cm.createUser(userid, password)
.on('success', function(){
// If everything runs smoothly, log them in
login_user(userid, password);
})
.on('conflict', function(data){
// If the email is already in use, show the error given by the server
show_error('login', data.errors[0]);
})
.on('badrequest', function(data){
// If the email is invalid, show our own error message instead
show_error('login', 'Please enter a valid email.');
})
.on('error', function(){
// In the event of any error at all reset the UI so they can try again
$('#register_button').attr('value', 'Register');
$('#login_button, #or').show();
});
// With CloudMine, you choose your own unique key for each object you create.
// Here we'll use the timestamp of the millisecond it was created.
var key = new Date().getTime();
/*
* After adding the key variable as __id__,
* the object_data looks like this:
* {
* "text": "test",
* "priority": 1,
* "picture": null,
* "__class__": "TBTodoItem",
* "deadline": {
* "__class__": "datetime",
* "timestamp": null
* },
* "location": null,
* "__id__": 1338503587770,
* "done": false
* }
*/
// key, value
cm.set(key, object_data)
.on('success', function(){
// Put the new item on the top of the list
draw_new_item(object_data);
});
cm.update(key, { done: true })
.on('success', function(){
// Tick off the item with a class that gives it a strikethrough, for example
$('[item="' + object_data.__id__ + '"').addClass('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment